我有这个链接
link_to "Ban", group_banned_user_path(:group_id => object.group_id, :user_id => object.user.id), :method => :post
触发此控制器操作
def create
@banned_user = BannedUser.new
user = User.find(params[:user_id])
group = Group.find(params[:group_id])
group.ban(user)
redirect_to group
end
通过这条路线
resources :groups do
member do
post :join
delete :leave
end
resources :banned_users, :only => [:create, :destroy]
end
但是我收到“No route matches [POST]”/ groups / 4 / banned_users / 4“,因为在create action url中没有/:id选项。所以我的问题是如何访问这两个用户和来自控制器操作的组ID,因为在link_to中作为单独的参数传递它们不起作用
答案 0 :(得分:0)
您的问题是当您拥有路线/groups/:id/banned_users/:banned_user_id
时,它基本上需要以下参数:params[:id]
params[:banned_user_id]
-
您正在发送此路线:
group_banned_user_path(:group_id => object.group_id, :user_id => object.user.id)
这不会填充:id
字段。你最好这样做:
group_banned_user_path(object.group_id, object.user.id)
-
<强>更新强>
我认为您的问题的答案是手动声明您的路线以接受:id
参数:
#config/routes.rb
resources :groups do
post :join #-> you don't need member
delete :leave
resources :banned_users, only: [] do
post ":id", action: "create" #-> groups/:id/banned_users/:id
delete ":id", action: "destroy"
end
end
这将使您能够使用我建议的路线