ruby-on-railsrubyruby-on-rails-5

What does f.object do in the Rails form builder?


I am learning Rails 5.0 from a tutorial, and in that tutorial it uses f.object, which I am unfamiliar with. The f.object is being passed into ERb, into a method that handles error processing.

I know that f is the object/instance of a record being passed into the form. But what I don't understand is f.object.

edit.html.erb (file with form):

<%= form_for(@section) do |f| %>

<%= error_messages_for(f.object) %>
  <table summary="Subject form fields">
    <tr>
      <th>Name</th>
      <td><%= f.text_field(:name) %></td>
    </tr>
    <tr>
      <th>Position</th>
      <td><%= f.select(:position, 1..@subject_count) %></td>
    </tr>
   </table>
<% end %>

There is no HTML form element known as object, and that's what usually goes after the f., so really miffed on what it could be.


Solution

  • f.object refers to the object that was passed as an argument to the form builder form_for method in this line <%= form_for(@section) do |f| %> in the view.

    In your example, f.object returns @section.