Jump to content

The ultimate community for Ruby on Rails developers.


form builder erb question

  • Please log in to reply
1 reply to this topic

#1 orrymr



  • Members
  • 12 posts
  • LocationJohannesburg, South Africa

Posted 20 January 2014 - 06:31 AM

I'm busy going through the guide on http://guides.rubyon...ng_started.html - I'm a bit stuck at the form builder part; I don't understand the following code: 

<%= form_for :post do |f| %>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
    <%= f.submit %>
<% end %>
  • In the first line, is form_for a method which takes the symbol :post as a parameter? What appears to happen next is that we start a block (which is basically an anonymous function, right?) and f is what is returned by the form_for method; it is the actual form builder object which creates our form on the page? Why does form_for take :post as a parameter? If I change it, it only seems to change the text on the submit button.
  • With all the f.label/f.text_field/f.submit line, why are symbols being put after them? Okay, maybe that was a bit obscure, what I mean is, for example, with "f.label :title" are we saying: "take the form builder object, call a method named label and give it the parameter :title, which is also a symbol for some reason."




#2 stevieing



  • Members
  • 40 posts

Posted 20 January 2014 - 03:49 PM

Have a look at the Rails documentation http://api.rubyonrai...thod-i-form_for which gives a pretty good explanation.


passing :post as a parameter does a bit more than changing the name on the submit button. Have a look at the html source it produces and you will see.


If you want a bit more understanding of how the code works then you can look at Ruby blocks. Also the Rails source code can give you some insight.


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users