Home > Back-end >  Ruby on Rails, disable a text field in a form only if action new
Ruby on Rails, disable a text field in a form only if action new

Time:09-21

I have a form partial that I include/render in both new.html.erb and edit.html.erb

I need to disable the email text field only if this partial is called via edit.html.erb

That is, a user enters an email during the sign up process, but afterwards a user cannot edit email anymore...

How do I make such a distinction?

Currently, I have this in my _form partial:

...

<%= f.label :email %> <%= f.email_field :email, class: 'form-control', disabled: true%>

...

email is disabled on both Sign up page and Edit page.

Thank you in advance

CodePudding user response:

You can try this:

<%= f.label :email %> 
<%= f.email_field :email, class: 'form-control', disabled: !f.object.new_record?%>
  • Related