How is it possible to preselect a country in the dropdown list generated by the country_select or localized_country_select helpers? The following code nicely creates a list of countries, but the :selected => :ch part does not have the expected effect of preselecting Switzerland (ch):
<%= localized_country_select(:user, :country, [], :selected => :ch) %>
Update: the following seems to be a better choice for achieving this.
localized_country_select_tag(name, selected_value = nil, priority_countries = nil, html_options = {})
Related
I have working on project using ruby on rails. I have created a form with many fields and one field is for jobs where I want to choose more then one job but now I am not able to choose more then one job with following code. Working with ruby on rails 3 and HAML. I also mention multiple true but still its not working.
= f.select :jobs, options_from_collection_for_select(Demojob.all, 'name','name' ), :multiple => true
If you check the implementation of the form builder select method (github), you'll see that the method signature is:
select(method, choices = nil, options = {}, html_options = {}, &block)
multiple flag should be passed using html_options hash, not options. In your case, it should be:
f.select :jobs, options_from_collection_for_select(Demojob.all, 'name', 'name'), {}, :multiple => true
Or, even better, if you prefer new hash syntax:
f.select :jobs, options_from_collection_for_select(Demojob.all, 'name', 'name'), {}, multiple: true
Lastly, there's no need to use options_from_collection_for_select with form builder, you can simply pass the options as arrays:
f.select :jobs, Demojob.all.collect { |job| [job.name, job.name] }, {}, multiple: true
Cheers!
I currently have a multiple select box in a rails form that looks like this:
= select_tag :in_all_tags, options_from_collection_for_select(Tag.where(:project_id => #project.id), :id, :name, #in_all_tags_param), { :id => "tags", :tabindex => "3", "data-placeholder" => "Choose Tags", :multiple => "multiple" }
Where
#in_all_tags_param = params[:in_all_tags]
The problem is, #in_all_tags_param will only populate the select form with the last value from params[:in_all_tags]. So, if the url string reads in_all_tags=5&in_all_tags=8, the pre-selected value in the multiple select will only be 8.
From what I understand, the way around this is to append [] to the field name for multiple params, so that :in_all_tags becomes in_all_tags[]
BUT, when I try this, submitting the form returns:
Expected type :default in params[:in_all_tags], got Array
Any suggestions appreciated.
Cheers...
You need to add a :name element to the same hash with :multiple => true in it. So I use something similar for Genres on an app for mine and I do { :multiple => true, :name => "lesson[genre_ids][]" }. The name has to be model[attribute][].
I'm using an out-dated version of rails (2.2).
I have a page which has a search filter. When I filter the options, I would like the Dropdown boxes to default to the filters I selected. The filters are being stored as parameters in the URL. i.e. filter[Issue+Header]=test&filter[in4User]=1&filter[Module]=3
What I search:
http://i.stack.imgur.com/r804l.png
What I currently see when page loads (as you can see, text boxes are re-populated, but dropdowns are not):
http://i.stack.imgur.com/G83X8.png
What I want to see when page loads:
http:// [remove_this_space] i.stack.imgur.com/r804l.png
Example of a collection_select I am using:
<%= collection_select(:filter, "Client", Client.find(:all, :conditions => ['status = 0']), :ClientID, :Name, :include_blank => true) %>
What you need to do is pass in the :selected option into collection select, and pass the appropriate param as the value, so something like:
<%= collection_select(:filter, "Client", Client.find(:all, :conditions => ['status = 0']), :ClientID, :Name, :include_blank => true, :selected => params[:filter]) %>
That should select the client, assuming that the Client is in the collection.
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.
Sorry, this may seem like a simple issue, but: I have a collection_select element that is called via ajax from a _updateregions.html.erb file for creating and editing records that looks like:
<%= collection_select(:wine, :wineregionid, regions, :wineregionid, :regionname,
options = {:selected => :wineregionid, :prompt => "Select a Region"}
) %>
The problem is, when editing an existing record, the prompt is appearing by default instead on the records value. When I remove the :prompt, it works fine... question is, how can I make this work for both the New and Edit case?
According to http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html I think I'm doing it right....
collection_select(object, method,
collection, value_method, text_method,
options = {}, html_options = {})
Returns and tags for
the collection of existing return
values of method for object‘s class.
The value returned from calling method
on the instance object will be
selected. If calling method returns
nil, no selection is made without
including :prompt or :include_blank in
the options hash.
i think :prompt donot takes a string .
it should be true/false or null.
try this
<%= collection_select(:wine, :wineregionid, regions, :wineregionid, :regionname,
options = {:selected => :wineregionid, :prompt => true) %>