Ruby-on-rails Rails:link_to 调用控制器中的自定义方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14262793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: link_to calls custom method in controller
提问by Rahul
I am looking to use link_toto call a method in my controller. However, for some odd reason the route looks for the showmethod.
我希望用来link_to在我的控制器中调用一个方法。但是,出于某种奇怪的原因,该路由会查找该show方法。
In my view:
在我看来:
<% @beverages.each do |beverage| %>
..
<%= link_to 'Archive', beverages_archive_path(:id => beverage.id) %>
..
<% end %>
In my config/routes.rb
在我的 config/routes.rb
match 'beverages/archive' => 'beverages#archive'
In my beverages_controller.rb
在我的 Drinks_controller.rb
def archive
beverage = Beverage.find(params[:id])
respond_to do |format|
# format.html # show.html.erb
format.json { render json: beverage }
end
# beverage.update_attribute('archive', true)
end
When I click on the archive link in the view, the URL does change to: http://localhost:3000/beverages/archive?id=11, however I get the following error.
当我单击视图中的存档链接时,URL 确实更改为: http://localhost:3000/beverages/archive?id=11,但是出现以下错误。
The error I get:
ActiveRecord::RecordNotFound (Couldn't find Beverage with id=archive):
app/controllers/beverages_controller.rb:46:in `show'
我得到的错误:
ActiveRecord::RecordNotFound (Couldn't find Beverage with id=archive): app/controllers/beverages_controller.rb:46:in `show'
Any idea on what I am doing wrong? Your help is much appreciated!
知道我做错了什么吗?非常感谢您的帮助!
PS. I also looked at Rails 3 link_to delete destory method calls show method?but nothing seemed to work.
附注。我还看了Rails 3 link_to delete destory 方法调用show 方法?但似乎没有任何效果。
采纳答案by dchacke
Have you tried this in your routes?
你在你的路线中试过这个吗?
match 'beverages/:id/archive' => 'beverages#archive', as: :beverages_archive
This will create the beverages_archive_path method for you. Also, as you are looking for a specific beverage, use :id inside the route so that it knows where to take the id parameter from.
这将为您创建 Drinks_archive_path 方法。此外,当您正在寻找特定的饮料时,请在路由内使用 :id 以便它知道从何处获取 id 参数。
Apart from that, you can always tell a link specifically which controller and action to link to by doing:
除此之外,您始终可以通过执行以下操作来具体告诉链接要链接到哪个控制器和操作:
link_to "Label", :controller => :my_controller, :action => :index
Taken from here: Ruby on rails 3 link_to controller and action
回答by MurifoX
Use the other notation (not match) instead.
请改用其他符号 (not match)。
resources :beverages do
collection do
get :archive
end
end
Try this one out and let me know if something went wrong.
试试这个,如果出现问题,请告诉我。
回答by gregates
There's not quite enough information here to know why beverages_archive_pathworks in your app -- one problem is that your routes file does not define a name for your beverages#archiveroute. What you want is something like:
这里没有足够的信息来了解为什么beverages_archive_path在您的应用程序中有效——一个问题是您的路由文件没有为您的beverages#archive路由定义名称。你想要的是这样的:
match 'beverages/archive' => 'beverages#archive', :as => :beverages_archive
or better yet use resourceful routing conventions like so:
或者更好地使用资源丰富的路由约定,如下所示:
resources :beverages do
collection do
get :archive
end
end
What's happening is that you have a beverages#showroute that matches /beverages/:id, and this is what /beverages/archivematches (with :id => 'archive').
发生的事情是你有一个beverages#show匹配的路线,/beverages/:id这就是/beverages/archive匹配(与:id => 'archive')。

