使用rails4中的form_for在表单操作中生成错误的URL

时间:2013-08-18 15:59:11

标签: ruby-on-rails ruby ruby-on-rails-4 form-for

我正在使用rails 4.0.0。我是铁路和学习铁路的新手,观看视频教程并陷入困境,因此需要一些帮助。

我有一个名为Subjects的控制器,以下是代码

主题构建器

class SubjectsController < ApplicationController

  before_action :set_subject, only: [:show, :edit, :update]

  def index
    @subjects = Subject.all
  end

  def list
    @subjects = Subject.all
    #@subjects = Subject.order("subjects.position ASC")
  end 

  def show
    @subject = Subject.find(params[:id])
  end

  def new
    @subject = Subject.new(:name => "default")  
  end

  def create
    @subject = Subject.new(subject_params)
        if @subject.save
        redirect_to(:action => 'list')
    else
        render('new')
    end
  end

  def edit

  end

  def update
    if @subject.update(subject_params)
        redirect_to @subject
    else
        render("edit")
    end
  end

  def delete
    @subject = Subject.find(params[:id])
  end

  def destroy   
    Subject.find(params[:id]).destroy
    redirect_to(:action => "list")
  end

  private

  # Use callbacks to share common setup or constraints between actions.
    def set_subject
      @subject = Subject.find(params[:id])
    end

  # Never trust parameters from the scary internet, only allow the white list through.
    def subject_params
      params.require(:subject).permit(:name, :position, :visible)
    end

end

我为列表操作

创建了一个视图

查看列表操作

<h1>List</h1>
<div class="subject list">
  <h2>Subjects</h2>

  <table class="listing" summary="Subject list">
    <tr class="header">
      <th>Position</th>
      <th>Subject</th>
      <th>Visible</th>
      <th>Pages</th>
      <th>Actions</th>
    </tr>
    <% @subjects.each do |subject| %>
    <tr>
      <td><%= subject.position %></td>
      <td><%= subject.name %></td>
      <td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
      <td class="center"><%= subject.pages.size %></td>
      <td class="actions">
        <%= link_to 'Show', subject, :class => 'action show'%>
        <%= link_to "Edit", edit_subject_path(subject), :class => 'action edit' %>
        <%= link_to "Delete", {:action => 'delete', :id => subject.id}, :class => 'action delete' %>
      </td>
    </tr>
    <% end %>
  </table>
</div>

我也有删除视图,代码如下

查看删除操作

<h1>Delete</h1>
<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>

<div class="subject delete">
  <h2>Delete Subject</h2>

  <%= form_for(:subject, :url => {:action => "destroy", :id => @subject.id}) do |f|%>

    <p>Are you sure you want to permanently delete this subject?</p>

    <p class="reference-name"><%= @subject.name %></p>

    <div class="form-buttons">
      <%= submit_tag("Delete Subject") %>
    </div>

  <% end %>
</div>

以下是我的路线文件的配置

SimpleCms::Application.routes.draw do  
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root 'welcome#index'
  root 'demo#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'
  get 'demo/index'
  get 'demo/hello'
  get 'demo/other_hello'
  #get 'subjects/index'
  get 'subjects/list'
  get 'subjects/delete'
  get  '/subjects/:id/destroy' => 'subjects#destroy'
  resources :subjects
  get 'subjects/show'
  get 'subjects/new'
  get 'subjects/:id' => 'subjects#show'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products
  #resources :subjects

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end

所以主要概念如下,当我打电话给 http://localhost:3000/subjects/list 时,它会显示所有主题,还有三个按钮显示,< strong>修改和删除,点击删除链接后,它会转到删除视图,点击表单的提交按钮后,它将会是删除特定项目。

但主要问题是删除视图中的 form_for 正在表单操作中生成 url / subjects / 8 ,其中 8 是要删除的主题的ID,但网址应该是 / subject / destroy? id = 8 要删除该项,但 id form_for 中的操作一起传递时,会产生错误的网址,但是只有动作传递它才会生成URL作为 / subjects / destroy

我无法弄清楚出了什么问题所以请帮忙。感谢所有提前。

2 个答案:

答案 0 :(得分:3)

尝试使用删除方法的链接,而不是使用表单。

<p>Are you sure you want to permanently delete this subject?</p>

<p class="reference-name"><%= @subject.name %></p>

<%= link_to 'Delete Subject', @subject, method: :delete %>

更新

如果您想使用表单进行销毁操作,请尝试以下操作:

<% form_for @subject, :html => {:method => :delete} do |form| %>

答案 1 :(得分:0)

试试这个:

<%= form_for(@subject, :url => {:action => "destroy") do |f|%>

而不是:

<%= form_for(:subject, :url => {:action => "destroy", :id => @subject.id}) do |f|%>

您不需要提及id,因为在form_for中它默认使用提到的变量的id。