button_to :action =>'destroy' 寻找 'show' [英] button_to :action => 'destroy' looks for 'show'

查看:36
本文介绍了button_to :action =>'destroy' 寻找 'show'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在构建一个 rails 应用程序,我正在尝试使用 button_ 来触发不同控制器中的销毁.

我的按钮代码是

<前><%= button_to "删除", :controller => :meals,:action => '摧毁',:recipe_id => recipe.id,:方法 => :post >

当我点击删除按钮时,我得到一个'没有匹配的膳食/3',这是当前的膳食 ID.

膳食控制器中的销毁看起来像这样

<前>销毁@meal = Meal.where("current_user.id => ? AND recipe_id => ?", current_user.id, params[:recipe_id]).first@meal.destroyresponse_to do |格式|format.html { redirect_to :controller => "user" , :action => "show" }format.xml { 头:ok }结尾结尾

似乎 button_to 完全忽略了 :action 并请求显示不存在且不应该存在的显示.

解决方案

那你的 route.rb 部分是什么样的?因为如果你使用 map.resources 然后 destroy 和 show 有相同的路径但是 :method =>:delete(这是由form和_method=delete参数实现的虚拟动词).

试试这个:

<%= button_to "delete", {:controller =>: 饭,:动作=>'销毁', :id =>recipe.id }, :method =>:删除%>

或者如果 recipeMeal 类的实例,则

<%= button_to "delete", @recipe, :method =>:删除%>

注意大括号.

This seems incredibly similar to a question I had answered just a few days ago, but the solution then isn't working now.

I'm building a rails app, and I am trying to have a button_to trigger a destroy in a different controller.

the code I have for the button is

<%= button_to "delete", :controller => :meals, 
                           :action => 'destroy',
                           :recipe_id => recipe.id,
                           :method => :post >

when I click the delete button, i get a 'no matches for meals/3' which is the current meal_id.

the destroy in the meals controller looks like this

  def destroy
    @meal = Meal.where("current_user.id => ? AND recipe_id => ?", current_user.id, params[:recipe_id]).first
    @meal.destroy

    respond_to do |format|
      format.html { redirect_to :controller => "user" , :action => "show" }
      format.xml  { head :ok }
    end
  end

it appears as though the button_to is completely ignoring the :action and requesting show which does not exist and shouldn't exist.

解决方案

And how you part of routes.rb for that one looks like? Because if you use map.resources then destroy has same path as show but :method => :delete(which is virtual verb implemented by form and _method=delete param).

Try this:

<%= button_to "delete", {:controller => :meals,
      :action => 'destroy', :id => recipe.id }, :method => :delete %>

or if recipe is instance of Meal class then

<%= button_to "delete", @recipe, :method => :delete %>

Mind the curly brackets.

这篇关于button_to :action =>'destroy' 寻找 'show'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆