ruby-on-rails - button_to :action => 'destroy' looks for 'show'

标签 ruby-on-rails

这似乎与我几天前回答的一个问题非常相似,但当时的解决方案现在不起作用。

我正在构建一个 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 %>

或者如果 recipeMeal 的实例然后上课
<%= button_to "delete", @recipe, :method => :delete %>

注意大括号。

关于ruby-on-rails - button_to :action => 'destroy' looks for 'show' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3763031/

相关文章:

ruby-on-rails - ActiveScaffold 可以与 Rails 3 一起使用吗?

ruby-on-rails - Rails 表单与 HTML/CSS 模板联姻

ruby-on-rails - 如何为这个公共(public)方法编写规范(使用 RSpec)?

ruby-on-rails - 在 rspec rails 中放置长模拟/ stub 响应的位置

ruby-on-rails - ROR 中的路由错误

ruby-on-rails - git push heroku master 错误权限被拒绝(公钥)

ios - rails : How to authenticate user on mobile native with Devise+Omni-facebook+Devise_token_auth after obtaining the access token

ruby-on-rails - 从 Controller 外部销毁资源

sql - Rails 中具有多个连接的高级查询

mysql - 无法使用 RubyMine 连接到正确的 mysql 安装