这似乎与我几天前回答的一个问题非常相似,但当时的解决方案现在不起作用。
我正在构建一个 rails 应用程序,并且我正在尝试使用 button_to 在不同的 Controller 中触发销毁。
我的按钮代码是
<%= button_to "删除", :controller => :meals,
:action => '摧毁',
:recipe_id => recipe.id,
:方法 => :post >
当我点击删除按钮时,我得到一个
'没有匹配的膳食/3',这是当前的膳食 ID。
膳食 Controller 中的销毁看起来像这样
销毁
@meal = Meal.where("current_user.id => ? AND recipe_id => ?", current_user.id, params[:recipe_id]).first
@meal.destroy
response_to do |格式|
format.html { redirect_to :controller => "user", :action => "show"}
format.xml { 头:ok }
结尾
结尾
似乎 button_to 完全忽略了 :action 并请求不存在也不应该存在的节目。
请您参考如下方法:
你如何为那一个路由的一部分看起来像?
因为如果你使用 map.resources 然后 destroy 和 show 有相同的路径但是 :method => :delete
(这是由 form 和 _method=delete 参数实现的虚拟动词)。
尝试这个:
<%= button_to "delete", {:controller => :meals,
:action => 'destroy', :id => recipe.id }, :method => :delete %>
或者如果
recipe
是
Meal
的实例然后上课
<%= button_to "delete", @recipe, :method => :delete %>
注意大括号。