author avatar

giritharan

Tue Jul 16 2024

The fields_for helper in Rails creates form bindings without rendering a
tag. This is particularly useful for rendering fields for additional model objects within a single form.

Imagine you have a Book model with an associated Author model. You can create a single form for both the Book and Author models using the fields_for helper.



<%= form_with model: @book do |book_form| %>
  <%= book_form.text_field :title %>
  <%= book_form.text_area :description %>

  <%= fields_for :author, @book.author do |author_form| %>
    <%= author_form.text_field :name %>
    <%= author_form.text_field :email %>
  <% end %>
<% end %>


#rails #fields-for #rails-view