0
0
RailsDebug / FixBeginner · 4 min read

How to Handle Form Submission in Ruby on Rails Correctly

In Ruby on Rails, handle form submission by creating a form with form_with helper and defining a matching controller action to process the submitted data. The controller action should use strong parameters to safely access form inputs and then save or update the model accordingly.
🔍

Why This Happens

Often beginners forget to connect the form to the correct controller action or miss using strong parameters, causing errors or no data saving. Another common mistake is using incorrect form helpers or missing the method attribute, leading to unexpected behavior.

erb and ruby
<%= form_with url: '/users', method: :post do |form| %>
  <%= form.text_field :name %>
  <%= form.submit 'Create User' %>
<% end %>

# In UsersController
class UsersController < ApplicationController
  def create
    @user = User.new(params[:user])
    if @user.save
      redirect_to @user
    else
      render :new
    end
  end
end
Output
ActionController::ParameterMissing (param is missing or the value is empty: user)
🔧

The Fix

Use form_with model: or ensure the form's url and method match the controller action. In the controller, use strong parameters with params.require(:user).permit(...) to safely access form data. This prevents errors and allows saving the data correctly.

erb and ruby
<%= form_with model: @user, local: true do |form| %>
  <%= form.text_field :name %>
  <%= form.submit 'Create User' %>
<% end %>

# In UsersController
class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to @user
    else
      render :new
    end
  end

  private

  def user_params
    params.require(:user).permit(:name)
  end
end
Output
Form submits successfully, user is saved, and browser redirects to user's show page.
🛡️

Prevention

Always use form_with model: for forms tied to models to get automatic URL and method handling. Use strong parameters in controllers to whitelist allowed fields. Test forms by submitting with browser DevTools open to check network requests and parameters sent.

Use Rails linters and code analyzers to catch missing strong parameters or incorrect form usage early.

⚠️

Related Errors

  • Unpermitted parameters: Happens when strong parameters do not include submitted fields; fix by adding them to permit.
  • Routing errors: Occur if form url or method does not match any route; fix by checking rails routes.
  • CSRF token errors: Caused by missing authenticity token in forms; fix by using Rails form helpers which add it automatically.

Key Takeaways

Use form_with model: to create forms linked to models automatically.
Always use strong parameters in controllers to safely handle form data.
Match form url and method with controller routes.
Test form submissions with browser DevTools to debug parameter issues.
Use Rails built-in helpers to avoid CSRF and routing errors.