Rails nested forms -> different treatment depending on value -
i have tricky question regarding nested forms. have following models:
- restaurant
- employees, in employees belongs_to restaurant.
i have 2 types of employees within employees model,
- managers
- staff
the managers
designated via manager
attribute in employees
model, boolean
.
when go edit list of employees each restaurant, through nested form,i.e:
<%= f.simple_fields :employees |f| %> <%= render 'employee_fields', :f => f %> <% end %>
my question: there way render different partial forms depending on type of employee? example, if current employee manager,
<%= render 'manager_fields', :f => f %>.
otherwise, if current employee staff,
<%= render 'staff_fields', :f => f %>.
yes, using ternary operator :
<%= render f.object.manager? ? 'manager_fields' : 'staff_fields' , :f => f %>
if can change design, have better solution you. follows :
use rails enum feature.
add field desgination
integer field in employee
model using migration.
then inside model:
enum designation: [ :staff, :manager ]
then storing in column designation
, either manager
( value 1) or staff
(value 0). inside view call <%= render f.object.desgination , :f => f %>
. , should have _manager.html.erb
, _staff.html.erb
2 partials.
and done!
Comments
Post a Comment