在控制器中执行操作的 Rails 3 链接或按钮
在 RoR 3 中,我只想有一个链接/按钮来激活控制器中的某些操作/方法。具体来说,如果我单击页面上的“update_specs”链接,它应该转到我的产品控制器中的“update_specs”方法。我在此站点上找到了执行此操作的建议:
link_to "Update Specs", :controller => :products, :action => :update_specs
但是,当我单击此链接时,出现以下路由错误:
路由错误没有路由匹配{:action=>"update_specs", :controller=>“产品”}
我已经阅读了有关路由的内容,但我不明白为什么如果所有其他方法都可以通过资源访问:产品,我应该路由此方法。
In RoR 3, I just want to have a link/button that activates some action/method in the controller. Specifically, if I click on a 'update_specs' link on a page, it should go to 'update_specs' method in my products controller. I've found suggestions to do this on this site:
link_to "Update Specs", :controller => :products, :action => :update_specs
However, I get the following routing error when I click on this link:
Routing Error No route matches {:action=>"update_specs",
:controller=>"products"}
I've read up on routing but I don't understand why I should have to route this method if all other methods are accessible via resources:products.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要为其创建一条路线。
例如:
默认情况下,
link_to
也会在路由中查找GET
方法。如果您想处理POST
或PUT
方法,您需要通过添加{:method => 来指定它。 :post }
或{:method => :put }
作为参数,例如:或者您可以使用
button_to
而不是link_to
,它默认处理POST
方法。You need to create a route for it.
For instance:
Also by default
link_to
will look for aGET
method in your routes. If you want to handle aPOST
orPUT
method you need to specify it by adding{:method => :post }
or{:method => :put }
as a parameter, like:Or you can use
button_to
instead oflink_to
which handles thePOST
method by default.