Rails Multiple Select boxes: Injecting default values from params - ruby-on-rails

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][].

Related

Array returns first element blank in my Rails(3.2.11) multi-select

When I selected multiple values from select list then array returns the first value empty.
= f.select :assignedto, options_from_collection_for_select(User.all, 'name', 'name',f.object.assignedto),{}, { :multiple => true}
I tried with {:include_blank => false} and {:include_hidden => false} but this is not working for rails 3.2.11. I have many solutions to handle this empty value in the controller but I want to stop adding empty value in the array.
Properly because the second arg is 'name' instead of 'id'.
Try
options_from_collection_for_select(User.all, 'id', 'name', f.object.assignedto)

Set :selected value based on variableX -or- the user submitted value if variableX is nil

Have this select dropdown input field that is populated with state names.
I want to set the selected value to be the content of #state.id if this is not nil. If there is already a chosen option then I would like that to be selected instead.
How can this be done correctly? I tried several solution all of which fails, latest code example im using now:
= p.input :state,
:collection => ["State1","State2"]
:selected => (#state.id.to_s rescue nil) || (params[:state] rescue nil),
:id => "state",
:name => "state",
:prompt => t('forms.choose')
The problem is that the :selected option works based on two values.
The index of the element in the collection array, or
The value itself
So lets say you want the the last option to be selected, in your case you would need:
:selected => 1
or
:selected => "State2"
I hope it helps.

ruby on rails select collection, filter results from array [enumerable]

current i am trying to restrict the information feed into an option select field to only display the criteria i have selected. with the code below this seems to be working
= select("schedule", :selected_players, User.where(:team_id => current_user[:team_id]) { |p| [full_name(p), p.id] }, {:include_blank => 'None', :prompt => 'Add Players to Lineup'}, :multiple => "multiple")
the issue is that this code is display an array field type i.e #<User:0xa559830>
how do i get it to display the actual users name?
Define .to_s method in model
Like here
https://github.com/roolo/mwstt/blob/master/app/models/project.rb#L7
Also all the mapping and searching logic should be placed in model as method which you'll just call in view, or prepare it in controller!

Ruby On Rails - Showing the default option with collection_select dependent on params

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.

Rails, collection_select - remembering values with :selected after form submitted

(Using Rails 2.3.5 on an internal work server with no choice of versions, and I'm pretty new)
I'm building a search form where I need to provide a list of directories to a user so they can select which one(s) to search against. I'm trying to figure out how to get the selected values of a collection_select to remain after the form is submitted.
Say the user selected 3 directories from the collection_select, the id's of those directories would look like this in the params:
directory: !map:HashWithIndifferentAccess
id:
- "2"
- "4"
- "6"
I know that you can manually specify multiple selected items:
<%= collection_select :directory, :id, #directories, :id, :name,
{:selected => [2,4,6]}, {:size => 5, :multiple => true} %>
I've also played around a bit and was able to us "to_i" against a single value in the params hash:
<%= collection_select :directory, :id, #directories, :id, :name,
{:selected => params[:directory][:id][0].to_i}, {:size => 5, :multiple => true} %>
What I can't figure out is how to use all of the values of the :directory params at the same time so what the user selected remains after the form is submitted. Thanks for any help.
I'm not precisely sure what you're asking, but if you're trying to get the array of strings in params[:directory][:id] as an array of integers, all you need is
params[:directory][:id].map{|id|id.to_i}

Resources