button_to:action =>’destroy’查找’show’
这似乎与我几天前回答的一个问题非常相似,但解决方案现在还没有奏效。
我正在构建一个rails应用程序,我正在尝试使用button_to在不同的控制器中触发destroy。
我对按钮的代码是
:meal, :action =>'destroy', :recipe_id => recipe.id, :method =>:post>
当我点击删除按钮时,我得到一个’不匹配饭/ 3’这是当前的meal_id。
膳食控制器中的破坏看起来像这样
def destroy @meal = Meal.where(“current_user.id =>?AND recipe_id =>?”,current_user.id,params [:recipe_id])。 @ meal.destroy respond_to do | format | format.html {redirect_to:controller =>“user”,:action =>“show”} format.xml {head:ok} 结束 结束
似乎button_to完全忽略了:action并请求show不存在且不应该存在。
你是如何将routes.rb的一部分看起来的? 因为如果你使用map.resources然后destroy有与show相同的路径但是:method => :delete
(这是由form和_method = delete param实现的虚拟动词)。
试试这个:
<%= button_to "delete", {:controller => :meals, :action => 'destroy', :id => recipe.id }, :method => :delete %>
或者如果recipe
是Meal
类的实例那么
<%= button_to "delete", @recipe, :method => :delete %>
注意大括号。
我知道这对答案来说太迟了但希望它可以帮助某人(使用Rails 4)。
<%= button_to "delete", meal_path(:id => recipe.id), :method => :delete %>