【发布时间】:2014-09-24 19:55:05
【问题描述】:
我有一个包含用户关注列表的表单,其中每个关注者都有一个相应的复选框。单击按钮将删除选定的以下内容。
followings 是用户下的嵌套资源,我的控制器中有一个 destroy 方法,当我执行 rake 路由时,我可以看到与 followings#destroy 操作对应的路由,但是当加载以下列表页面时,它不会抛出任何路由匹配错误。
routes.rb:
resources :users do
resources :followings
resources :events
end
following_controller.rb:
class FollowingsController < ApplicationController
def index
@followings = Following.findFollowings(params.has_key?("user_id") ? params[:user_id] : current_user.id)
@following = Following.new
end
def destroy
Following.any_in(:following_id => params[:id]).destroy_all
render index
end
end
index.html.haml:
= form_for(@following, url: {action: 'destroy'}, :html => {:method => :delete, :role => 'form'}) do |f|
- @followings.each do |following|
= f.check_box "following_id"
= f.submit "Delete"
耙路线:
user_followings GET /users/:user_id/followings(.:format) followings#index
POST /users/:user_id/followings(.:format) followings#create
new_user_following GET /users/:user_id/followings/new(.:format) followings#new
edit_user_following GET /users/:user_id/followings/:id/edit(.:format) followings#edit
user_following GET /users/:user_id/followings/:id(.:format) followings#show
PATCH /users/:user_id/followings/:id(.:format) followings#update
PUT /users/:user_id/followings/:id(.:format) followings#update
DELETE /users/:user_id/followings/:id(.:format) followings#destroy
错误堆栈:
没有路线匹配{:action=>"destroy", :controller=>"followings", :user_id=>"540f5c6b7072610a4c040000"}
提取的源代码(第 9 行附近):
6 %ul
7 = render partial: "shared/npo_menu", locals: {item: 'followings'}
8 %section.following.notifications
9 = form_for(@following, url: {action: 'destroy'}, :html => {:method => :delete, :role => 'form'}) do |f|
10 .container
11 .row.manipulate
12 .pull-left
谢谢!
【问题讨论】:
-
看起来你的缩进可能是关闭的,如果代码与你粘贴的相同。应该是缩进然后following.check_box?
-
表单布局看起来有点不寻常。你能解释一下为什么你在这个视图中使用两个不同的实例变量“@following”和“@followings”吗?另外,为什么要在索引视图中渲染表单(不是说它是错误的,但通常不是索引的用途)。
-
@Kevin 缩进在实际代码中是正确的,只是在这里粘贴了几行而不是整个haml。 “@followings”是要在索引页面上显示的列表。 “@following”是用户选择要删除的列表项。
标签: ruby-on-rails ruby-on-rails-4