bkowalczyyk
bkowalczyyk

Reputation: 292

Helper method in rails

I am currently learning RoR and I have problem with understanding helper-method's. This example is from ruby guides.

So, when I click submit form in this example I call create method in controller. But, when I click "back" button I go to index action.

<%= form_for :post, url: posts_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>
<% end %>

<%= link_to "Back", posts_path %>

Why with the same url posts_path (this is a helper method, isn't it ?) I have different results?

Upvotes: 0

Views: 69

Answers (3)

Nikos
Nikos

Reputation: 1062

The difference is that the form submits a POST request to posts_path while the back button a GET. This is the common REST way Rails handles its resources. Have a look at the rails docs for more info on that

Upvotes: 0

Logan Serman
Logan Serman

Reputation: 29880

The redirect uses a GET, while the form uses a POST.

Upvotes: 0

usha
usha

Reputation: 29369

HTTP method is different in your case

method: POST, posts_path -> create action
method: GET, posts_path -> index action

Looking at the result of rake routes | grep post will give you some idea

Upvotes: 1

Related Questions