How to Use form_with in Rails: Syntax and Examples
Use
form_with in Rails to create forms by passing a model or URL and options. It automatically handles form submission and generates HTML form tags with proper attributes. Use the block form to add form fields inside the form.Syntax
The basic syntax of form_with uses a model or URL and a block to define form fields. You can pass options like local: true to disable AJAX submission.
- model: The Active Record object the form is for.
- url: The path where the form submits.
- local: When true, submits with normal HTTP instead of AJAX.
- block: The form fields inside the form.
ruby
form_with(model: @article, local: true) do |form|
# form fields here
endExample
This example shows a form for a new Article model with fields for title and body. It uses form_with with local: true to submit normally.
ruby
class ArticlesController < ApplicationController def new @article = Article.new end def create @article = Article.new(article_params) if @article.save redirect_to @article else render :new end end private def article_params params.require(:article).permit(:title, :body) end end # In app/views/articles/new.html.erb <%= form_with(model: @article, local: true) do |form| %> <div> <%= form.label :title %><br> <%= form.text_field :title %> </div> <div> <%= form.label :body %><br> <%= form.text_area :body %> </div> <div> <%= form.submit "Create Article" %> </div> <% end %>
Output
<form action="/articles" accept-charset="UTF-8" method="post">
<div>
<label for="article_title">Title</label><br>
<input type="text" name="article[title]" id="article_title">
</div>
<div>
<label for="article_body">Body</label><br>
<textarea name="article[body]" id="article_body"></textarea>
</div>
<div>
<input type="submit" name="commit" value="Create Article" data-disable-with="Create Article">
</div>
</form>
Common Pitfalls
Common mistakes when using form_with include:
- Not setting
local: truewhen you want a normal form submit, which causes AJAX submission by default. - Forgetting to pass a model or URL, which leads to errors.
- Using
form_fororform_taginstead ofform_within new Rails versions.
Example of wrong and right usage:
erb
<!-- Wrong: AJAX submit when you want normal submit -->
<%= form_with(model: @article) do |form| %>
<!-- fields -->
<% end %>
<!-- Right: normal submit -->
<%= form_with(model: @article, local: true) do |form| %>
<!-- fields -->
<% end %>Quick Reference
Summary tips for using form_with:
- Use
model:for forms tied to Active Record objects. - Use
url:for forms not tied to models. - Set
local: trueto disable AJAX if you want normal form submission. - Use the block parameter to access form helpers like
text_field,text_area, andsubmit. - Rails 5.1+ prefers
form_withoverform_forandform_tag.
Key Takeaways
Use form_with with a model or URL and a block to create forms in Rails.
Set local: true to disable AJAX and submit forms normally.
Use form_with helpers inside the block to add input fields and buttons.
Avoid using deprecated form_for or form_tag in new Rails apps.
Always pass either a model or a URL to form_with to avoid errors.