在控制器中执行操作的 Rails 3 链接或按钮

发布于 2024-12-06 17:28:02 字数 422 浏览 1 评论 0原文

在 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤城病女 2024-12-13 17:28:02

您需要为其创建一条路线。

例如:

resources :products do
  put :update_specs, :on => :collection
end

默认情况下,link_to 也会在路由中查找 GET 方法。如果您想处理 POSTPUT 方法,您需要通过添加 {:method => 来指定它。 :post }{:method => :put } 作为参数,例如:

link_to "Update Specs", {:controller => :products, :action => :update_specs}, {:method => :put }

或者您可以使用 button_to 而不是 link_to,它默认处理 POST 方法。

You need to create a route for it.

For instance:

resources :products do
  put :update_specs, :on => :collection
end

Also by default link_to will look for a GET method in your routes. If you want to handle a POST or PUT method you need to specify it by adding {:method => :post } or {:method => :put } as a parameter, like:

link_to "Update Specs", {:controller => :products, :action => :update_specs}, {:method => :put }

Or you can use button_to instead of link_to which handles the POST method by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文