I am following the official ruby on rails tutorial and I just finished chapter 5.9.
Adding links should be simple but I am majorly confused.
When I type bin/rake routes
, I get the following output:
fl4m3ph03n1x: ~/blog $ bin/rake routes
Prefix Verb URI Pattern Controller#Action
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
root GET / welcome#index
fl4m3ph03n1x: ~/blog $
Which makes sense according to the tutorial.
To make use of this, I have a view:
<h1>New Article</h1>
<%= 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>
<% end %>
<%= link_to 'Back', articles_path %>
This view has a submit form and a link in the end.
According to ruby, I specify the submit button link in the form by using articles_path
in <%= form_for :article, url: articles_path do |f| %>
.
I really have no idea how that variable is set, but I will take the bait and accept it.
According to the tutorial, when clicking the submit button articles_path
will be "POST /articles(.:format) articles#create" by default.
However, in the link <%= link_to 'Back', articles_path %>
, articles_path
is supposed to redirect us to the index page...
Can someone explain me how the same variable has 2 radically different behaviors in the same View ??
link_to
default request type is 'GET'.
button_to
default request type is 'POST'.
Each generated route has a specific type, which is how rails map different requests to the correct ones.
For form_for
action view helper method, it differentiates between 'POST' and 'PUT' automatically depending on whether you passed an instance or not to the form.
you can also explicitly provide the method type for the form by adding
method: 'GET' OR :html => { :method => 'GET' }
** check different syntax capabilities depending on rails version.
Same goes to other methods, so if you want link_to
to send post request you have to pass method="POST"
to it.
In the generated routes table you may have noticed that index action does NOT need an instance id because it is supposed to list all articles. However, for show, you need to pass an instance to it, because it is supposed to show a specific instance only.
= link_to "index", articles_path
= link_to "show", article_path(article)
NOTICE ::
The two methods is not the same, "articles" and "article", plural vs singular. Even if they were identical in names one of them will take an instance and the other won't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With