jissy
jissy

Reputation: 463

rails 4 select with selected option not working

In my rails 4 form, I have one field to select values.

<%= f.select(:owner_user_id, Item.find(session[:login_users_item_id]).try(:users).order_by_fullname.collect {|u| [ u.full_name, u.id ] } , {:selected =>  current_user.full_name} )%>

But, the above selected option not showing the above value. In console that value is getting properly.Now,The first item is showing in the select box.

How can I get the value in the selected option as the default value?

Upvotes: 1

Views: 4447

Answers (3)

Amit Sharma
Amit Sharma

Reputation: 3477

You can also use options_for_select helper.

Example :

<%= f.select :owner_user_id, options_for_select(Item.find(session[:login_users_item_id]).try(:users).order_by_fullname.pluck([ :full_name, :id ]), f.object.owner_user_id) %>

Upvotes: 0

Vrushali Pawar
Vrushali Pawar

Reputation: 3803

<%= f.select(:owner_user_id, Item.find(session[:login_users_item_id]).try(:users).order_by_fullname.collect {|u| [ u.full_name, u.id ] }, selected: f.object.owner_user_id)%>

Try this

Upvotes: 2

Arslan Ali
Arslan Ali

Reputation: 17812

The third argument to f.select method is the selected value, and there you can pass the value that you would like to see selected. In your case: current_user.full_name

<%= f.select(:owner_user_id, Item.find(session[:login_users_item_id]).try(:users).order_by_fullname.collect {|u| [ u.full_name, u.id ] }, current_user.full_name)%>

Upvotes: 1

Related Questions