user3726986
user3726986

Reputation:

Understanding the code snippet in ERB

I have taken the code snippet from Rails GUides Book . What I am unable to understand is that how come articles_path redirect the page to two different pages. In first case it is redirecting to articles/5 and in second case it is redirecting to articles. How is this parameter changing the redirection 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 %>

Upvotes: 0

Views: 103

Answers (1)

usha
usha

Reputation: 29349

In the following case

<%= form_for :article ,url: articles_path do |f| %>

Its a http POST to articles_path

In

<%= link_to 'Back', articles_path %>

Its a http GET

so http verb determines the action in this case. When you do rake routes, you will see the following

articles GET /articles(.:format)     {:action=>"index", :controller=>"articles"}
         POST /articles(.:format)     {:action=>"create", :controller=>"articles"}

Upvotes: 2

Related Questions