HermannHH
HermannHH

Reputation: 1772

Only showing inputs for attributes that are not readonly on update

I have a model Foo with attributes bar and baz.

Baz is marked as attr_readonly as it's value cannot be changed after it has been created.

Is there a way to not display the baz input field in the partial firm rendered? This should be dynamic as this firm is also used by the create method, in which case the input will exist.

I am using simple_form and rails 4.

Upvotes: 0

Views: 40

Answers (1)

srecnig
srecnig

Reputation: 888

ActiveRecord's new_record? will tell you if the record has been persisted already, you can use that. so something like this should do for you:

<%= f.input :baz if foo.new_record? %>

there's also the persisted? method which does the same but gives you the inverse result.

Upvotes: 3

Related Questions