Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't display field of form if action is edit

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?

like image 900
Adnan Pirota Avatar asked Nov 08 '14 09:11

Adnan Pirota


4 Answers

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
like image 98
Ahmad Elassuty Avatar answered Nov 03 '22 19:11

Ahmad Elassuty


It can be done in many ways.

I usually do it like

if [email protected]_record?
 # department field.
end
like image 23
Nithin Avatar answered Nov 03 '22 21:11

Nithin


Just put at your form:

<%= f.text_field :department unless @user.new_record? %>
like image 2
Alejandro Babio Avatar answered Nov 03 '22 19:11

Alejandro Babio


If params[:action] is edit then do not show that field might be by using display none

like image 1
Triveni Badgujar Avatar answered Nov 03 '22 19:11

Triveni Badgujar