Reputation: 284
I have a select box in my form where I can choose a user from a list. This saves correctly and applies the user to my Chasing model, but I'm wondering if it's possible to have the select show the currently assigned user instead of having it reset every time I edit the object. My form code is:
<%= f.select :user_id, options_for_select(User.all.map {|c| [c.name, c.id]}), { :include_blank => "Please select user"}, {:class => "form-control"} %>
I've also made it so that the form needs the presence of user to be saved, however there may be occasions where I need to save a new Chasing with a default user (called 'Unassigned'). Is there a way of defaulting the select box to this user unless the Chasing already has a user assigned to it?
Any help with this would be greatly appreciated :)
Upvotes: 0
Views: 51
Reputation: 23661
You can pass :selected => value
as param
<%= f.select :user_id, options_for_select(User.all.map {|c| [c.name, c.id]}), { :include_blank => "Please select user"}, {:class => "form-control", :selected => o.customer_id } %>
Upvotes: 0
Reputation: 1484
Just add @object.field_name
in options_for_select
after collection
as below.
<%= f.select :user_id, options_for_select(User.all.map {|c| [c.name, c.id]}, @object.user_id), { :include_blank => "Please select user"}, {:class => "form-control"} %>
Hope this will fix you issue.
Upvotes: 1
Reputation: 4413
According to this: http://apidock.com/rails/v4.2.1/ActionView/Helpers/FormOptionsHelper/options_for_select
<%= f.select :user_id, options_for_select(User.all.map {|c| [c.name, c.id]}, current_user.id), { :include_blank => "Please select user"}, {:class => "form-control"} %>
(replace current_user
with your user object you want to place in)
Upvotes: 1