views:

176

answers:

1

I've a model that the user isn't allowed to update most fields on after the initial creation.

I've seen the :readonly HTML attribute I can tack on all field helpers, but doing conditionals on all fields feels... icky.

I'm not using anything special for creating my forms at the moment, just plain HAML. Anyone know of a better way of doing this?

This is what I've thought of doing 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 solution to make me drool would be a way to set an attribute as read-only on the model together with State Machine which then would propagate to the views. :)

A: 

Use attr_protected (from Bill Eisenhauer).

1st result from Googling rails constants.

mcandre
`attr_protected` just protects the value from being updated in the database. I'd rather not have my users spending time updating a field to have it not save without a warning. :) Thus I wanted to find a way to set the value of read-only on a specific state and then the view would know to set it as read-only. I'm at the moment using the `set_readonly?` helper I wrote in the question body.
ba