How to Use form_for in Rails: Syntax and Examples
In Rails,
form_for is a helper method used to create a form tied to a model object, automatically setting the form's action and method. You pass the model instance to form_for, and inside the block, use form builder methods like f.text_field to generate input fields linked to model attributes.Syntax
The form_for helper takes a model object and a block. Inside the block, a form builder variable (commonly f) is used to create form fields linked to the model's attributes.
- model: The instance of the model you want to build the form for.
- form builder (f): Used to generate input fields like text fields, checkboxes, etc.
- submit button: Adds a button to submit the form.
erb
<%= form_for(@article) do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :content %>
<%= f.text_area :content %>
<%= f.submit "Save" %>
<% end %>Example
This example shows a form for an Article model with title and content fields. When submitted, it sends data to the appropriate controller action to create or update the article.
erb
<!-- app/views/articles/_form.html.erb -->
<%= form_for(@article) do |f| %>
<div>
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div>
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div>
<%= f.submit "Save Article" %>
</div>
<% end %>Output
<form action="/articles" method="post">
<div>
<label for="article_title">Title</label><br>
<input type="text" name="article[title]" id="article_title">
</div>
<div>
<label for="article_content">Content</label><br>
<textarea name="article[content]" id="article_content"></textarea>
</div>
<div>
<input type="submit" name="commit" value="Save Article">
</div>
</form>
Common Pitfalls
Common mistakes when using form_for include:
- Not passing a model instance, which causes errors or unexpected behavior.
- Using
form_forwith a symbol instead of a model instance (useform_withfor symbols). - Forgetting to include the authenticity token, which Rails adds automatically with
form_for. - Not using the form builder methods (
f.text_field,f.label) inside the block, which breaks the connection to the model.
Example of wrong usage and correct usage:
erb
<!-- Wrong: passing symbol instead of model instance --> <%= form_for(:article) do |f| %> <%= f.text_field :title %> <% end %> <!-- Right: pass model instance --> <%= form_for(@article) do |f| %> <%= f.text_field :title %> <% end %>
Quick Reference
Tips for using form_for:
- Always pass a model instance to
form_for. - Use the form builder variable to create fields linked to model attributes.
- The form's action and method are set automatically based on the model's state (new or existing record).
- Use
f.submitto add a submit button. - For non-model forms or symbols, prefer
form_within Rails 5.1+.
Key Takeaways
Use form_for with a model instance to create forms tied to that model.
Inside the form_for block, use the form builder methods to generate input fields.
form_for automatically sets the form action and HTTP method based on the model state.
Avoid passing symbols to form_for; use form_with for non-model forms.
Always include a submit button using f.submit to allow form submission.