[英]Rails: link_to calls custom method in controller
I am looking to use link_to
to call a method in my controller. 我希望使用
link_to
来调用我的控制器中的方法。 However, for some odd reason the route looks for the show
method. 然而,由于一些奇怪的原因,该路线寻找
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 在我的beverages_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(无法找到带有id = archive的Beverage):app / controllers / beverages_controller.rb:46:在`show'中
Any idea on what I am doing wrong? 对我做错了什么的想法? Your help is much appreciated!
非常感谢您的帮助!
PS. PS。 I also looked at Rails 3 link_to delete destory method calls show method?
我还看了Rails 3 link_to删除destory方法调用show方法? but nothing seemed to work.
但似乎没什么用。
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. 这将为您创建beverages_archive_path方法。 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.
此外,当您正在寻找特定饮料时,请在路径中使用: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 从这里开始: Ruby on rails 3 link_to控制器和动作
Use the other notation (not match
) instead. 请改用其他符号(不
match
)。
resources :beverages do
collection do
get :archive
end
end
Try this one out and let me know if something went wrong. 尝试这个,让我知道是否出了什么问题。
There's not quite enough information here to know why beverages_archive_path
works in your app -- one problem is that your routes file does not define a name for your beverages#archive
route. 这里有不是很足够的信息,知道为什么
beverages_archive_path
在您的应用程序的工作原理-一个问题是,你的路由文件没有定义你的名字beverages#archive
路线。 What you want is something like: 你想要的是:
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#show
route that matches /beverages/:id
, and this is what /beverages/archive
matches (with :id => 'archive'
). 发生了什么事情,你有一个
beverages#show
route匹配/beverages/:id
,这就是/beverages/archive
匹配(用:id => 'archive'
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.