Rails reuses the form view for editing, but sets some read-only fields

I have a model that the user is not allowed to update most of the fields after the initial creation.

I saw an :readonlyHTML attribute that I can use for all field helpers, but meeting conditional conditions in all fields seems ... icky.

I am not using anything special to create my forms at the moment, just HAML. Does anyone know a better way to do this?

This is what I have been thinking about it so far:

def set_readonly?(object, html_attr)
  html_attr.merge(object.new_record? ? {} : {:readonly => 'readonly'})
end

Used as:

f.text_field :supplier_id, set_readonly?(@damaged_goods, {:size => 5})

The decision to make me salivate would be a way to set the attribute as read-only in the model along with the State Machine , which then extends to the views. :)

+5
2

<INPUT> new edit:

f.text_field :supplier_id, readonly: f.object.persisted?
+2

attr_protected ( Bill Eisenhauer).

1- Googling rails constants.

-3

All Articles