我从Rails GUides Book中获取了代码片段。我无法理解的是,articles_path如何将页面重定向到两个不同的页面。在第一种情况下,它重定向到文章/ 5,在第二种情况下,它重定向到文章。该参数如何更改重定向URL
<%= form_for :article ,url: articles_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<%= link_to 'Back', articles_path %>
<% end %>
答案 0 :(得分:3)
在以下情况
<%= form_for :article ,url: articles_path do |f| %>
它是一个http POST
到articles_path
在
<%= link_to 'Back', articles_path %>
它是一个http GET
所以http动词确定了这种情况下的动作。
执行rake routes
后,您会看到以下内容
articles GET /articles(.:format) {:action=>"index", :controller=>"articles"}
POST /articles(.:format) {:action=>"create", :controller=>"articles"}