rails 4 select with selected option not working - ruby-on-rails

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?

<%= 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

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)%>

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) %>

Related

include blank : true not working in select rails

<%= select :used_car, :car_make_id, #all_makes.map { |m| [m.title, m.name] }, {:include_blank => true,:prompt=>"Select Make"}%>
<%= select :used_car, :car_model_id, [], {:include_blank => true,:prompt=>"Select Model")}, size: 10, :class=>"form-control select"%>
Car models list updating on basis on car make selection
Issue is that when I select car make, prompt of car model remove and first option come
how i resolve it?
Here is the documentation.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
Try f.select :car_model_id, ... instead. Also try using nil for choices, or [[]] for choices.
But first, you have an extra stray parenthesis ')' in this line:
<%= select :used_car, :car_model_id, [], {:include_blank => true,:prompt=>"Select Model" ) }
You have to remove that parenthesis. It doesn't go with anything.

Rails grouped_options_for_select not populating edit action

I have a form with two fields where the second depends on what was selected in the first dropdown, if I select 'Asia' in the first, then 'Japan' and 'China' appear as options in the second dropdown.
.field
= f.label :country
= f.select :country, ['Asia', 'Europe'], :prompt => 'Select One'
.field
= f.label :category
= f.select :category,grouped_options_for_select(MyModel::CATEGORIES, nil, "Please Select")
The CATEGORIES variable looks like;
CATEGORIES = {
'Asia'=> [ 'Japan','China'],
'Europe'=> [ 'Ireland', 'France']
}
This works but when I go the edit page the second dropdown is not pre-populated with the stored value, how do I do this?
#dax is right but you need to explicitly set the selected value and not just the attribute. From the api
selected_key - A value equal to the value attribute for one of the tags, which will have the selected attribute set. Note: It is possible for this value to match multiple options as you might have the same option in multiple groups. Each will then get selected="selected".
Your code should be
= f.select :category, grouped_options_for_select(MyModel::CATEGORIES, f.object.category)
EDIT
The last argument passed to grouped_options_for_select should be passed to select instead and should be the value of either the prompt or include_blank option.
= f.select :category,
grouped_options_for_select(MyModel::CATEGORIES, f.object.category),
{ prompt: 'Please select' }, # here goes the select tag options
{ class: 'my-class' } # here goes the html options
you've set the selected key to nil -
MyModel::CATEGORIES, # nil #, "Please Select"
Try changing your code to this:
= f.select :category,grouped_options_for_select(MyModel::CATEGORIES, :country, "Please Select")

need to add default value in f.select field to existing ones - rails 3.2

With the code I have below in the select field I have all the public_campaigns:
<%= f.select :campaign_id, #public_campaigns.map{|x| [x.name,x.id]} %>
public_campaigns is defined in controller with:
#public_campaigns = #logged_in_user.campaigns.order('created_at desc')
In the form I select the campaign and fill up the rest of the form and at the submit action an invitation is created with campaign_id taken from the campaign I selected in the form, it can be anything from 1 to n
What I need now is to have a default item in select field that will have the value of 0 and named "No campaign", it means I invite someone to a campaign that I have not created yet and in invitation the campaign_id field will be 0.
Thank you for your time.
Do you really need 0? I think use of {:include_blank => "No campaign"} should be enough?
Try this:
<%= f.select :campaign_id, (#public_campaigns.map{|x| [x.name,x.id]} << ["No campaign",0]), {:selected => 0} %>
Well, the fastest way you can do this is something like this:
#public_campaigns = #logged_in_user.campaigns.order('created_at desc')
no_campaign = Campaign.new(:id => '0', :name => 'No Campaign')
#public_campaigns.unshift(no_campaign)
I'm not sure why you are unable to do it this way:
<%= f.collection_select :campaign_id, #public_campaigns, :id, :name, prompt: 'No campaign' %>
Just check if campaign_id.nil? instead of assigning any value to campaign_id

collection_select set option value of :include_blank to zero

In my rails app, i have a drop down box where i retrieve all groups from the Group table and display them using the collection_select tag.
When the user selects 'None', I want to pass '0' as option value.
Currently, an empty string is passed.
Is there a way to include option value = 0 for 'None'?
<%= f.collection_select :SUB_GROUP, Group.all, :Group_ID, :Group_ID, :include_blank => 'None' %>
Many many thanks for any suggestion provided
If you use options_for_select in combination with select_tag you can achieve that using this:
options_for_select(
[['None', '0']].concat(
Group.all.collect { |g| [g.group_id.to_s, g.group_id.to_s] }
)
)
In order to keep your views uncluttered, you might want to generalize and move this into a helper method with a reasonable name.

Ruby on Rails: how do I use a default placeholder thing in a select_tag?

<%= select_tag(:services,
options_from_collection_for_select(Service.all, :id, :name))%>
And it displays all the services...
But I want it to be something like:
Select a service
Service1
Service2
Service3
Service4
Most of the time, you don't want to append anything to the array directly; either of these is a cleaner solution:
Use :prompt => "Placeholder" if you want the placeholder to show up only when the attribute is nil at the time the form is rendered. It will be selected by default, but nothing will be saved if user submits. If the attribute is already populated [possibly because a) there's a default value or b) it's an edit form], the placeholder item will be omitted from the list entirely.
Use :include_blank => "Placeholder" if you want to include the placeholder in the rendered list at all times.
<%= select_tag(:services,
Service.all.collect { |c| [c.id, c.name] }.
insert(0, "Select a Service"))%>
As answered for the question, this is for Rails 2.3. For Rails 3, see Prathan Thananart's answer.
The better way to do this would be to use the :prompt parameter. Something like:
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:prompt => 'Select Person'})
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

Resources