Ruby-on-rails Rails 在 form_for 中更改提交的路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10414514/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:20:36  来源:igfitidea点击:

Rails change routing of submit in form_for

ruby-on-railsrubyruby-on-rails-3routingform-submit

提问by Lailo

I have a model 'Article' and a model 'Ratings' nested within articles.

我有一个模型“文章”和一个嵌套在文章中的模型“评级”。

/articles/123/ratings

/文章/123/评级

I want to change the routing of the f.submit in ratings/_form.html.erb now it is so, that after pressing submit, my application routs to

我想更改 ratings/_form.html.erb 中 f.submit 的路由,现在是这样,按下提交后,我的应用程序路由到

/ratings/111

/评分/111

but I want to route it to

但我想把它路由到

/article/123

/文章/123

How can I change the routing in a form_for f.submit button. I have found here something like this:

如何在 form_for f.submit 按钮中更改路由。我在这里找到了这样的东西:

<% form_for :thing, :url => 
 url_for(:action => "update", :id => @thing) do |f| %>

But this do not work for my rails 3.2. Thanks for your help,

但这对我的 rails 3.2 不起作用。谢谢你的帮助,

回答by Abid

:url - The URL the form is submitted to. It takes the same fields you pass to url_for or link_to. In particular you may pass here a named route directly as well. Defaults to the current action.

:url - 表单提交到的 URL。它采用您传递给 url_for 或 link_to 的相同字段。特别是,您也可以直接在此处传递命名路线。默认为当前操作。

<% form_for :thing, :url => {:action => "update", :id => @thing} do |f| %>

you can also pass it the path_name by using the helper. so you can also do something like

您还可以使用帮助程序将 path_name 传递给它。所以你也可以做类似的事情

:url => update_article_path(@article)

回答by Antonio Jha

Try form_for (:thing, url:{:controller=>'thing', :action=>'update'}, html:{method:'put'}).

试试form_for (:thing, url:{:controller=>'thing', :action=>'update'}, html:{method:'put'})