DEV Community

Cover image for readonly vs disabled in Rails Forms
Jess Alejo
Jess Alejo

Posted on

readonly vs disabled in Rails Forms

Today I found out that disabling a form field in Rails means it won't be submitted at all. I used disabled: true thinking I could show a value that users can’t change — and I could — but the downside is that the value doesn’t get sent to the controller.

I learned that if I want the value to be shown but not editable and still be passed in the form data, the correct attribute to use is readonly: true.

This small difference between disabled and readonly matters when you're depending on that data in your controller.

It’s not a Rails issue; it’s how forms work in general.

Quick Example

<%= form_with model: @user do |form| %>
  <!-- This won't send the value -->
  <%= form.text_field :email, disabled: true %>

  <!-- This will send the value -->
  <%= form.text_field :email, readonly: true %>
<% end %>
Enter fullscreen mode Exit fullscreen mode

This was a helpful thing to learn today. Hopefully it saves someone else a bit of time too.

Top comments (0)