I have rails form that renders partial with fields list to create new user. Those fields I am also using to render edit form. I wonder if it is possible not to show specific field (i.e. department) on the edit form, because I don't want to give users option to change the department?
You can use persisted? method to ensure that it is not a new record that will be inserted or even use new_record? method which will return true if the record is not persisted in your database "New record". ex ::
<%= f.text_field :department if @user.persisted? %>
or
<%= f.text_field :department unless @user.new_record? %>
one more way is to check about the action itself which also gives you control on the controller if you render the partial from different view and you want to limit it to specific controller, these are stored in ::
params[:controller] -->> contains the name of the controller ex. UserController that you just hit on
params[:action] -->> contains the action name ex. new or edit
It can be done in many ways.
I usually do it like
if [email protected]_record?
# department field.
end
Just put at your form:
<%= f.text_field :department unless @user.new_record? %>
If params[:action] is edit then do not show that field might be by using display none
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With