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

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.

Related

Rails Multiple Select boxes: Injecting default values from params

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

rails select, with restricted query

Currently 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 displaying an array field type i.e #<User:0xa559830>.
How do I get it to display the actual users name?
I suggest rewriting the code with greater separation and clarity instead of trying to fit it all in one line. Model methods that generate SQL should never be called from views.
In the controller:
#players = User.where(:team_id => current_user[:team_id]).all
In the view:
= select :schedule, :selected_players, #players.map {|p| [full_name(p), p.id] }, {:include_blank => true}...
I think the original error in your code appears to be that you were calling a block after the where method without an iterator (each).

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!

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}

Not setting anything in Rails' collection_select

I have a Rails 2.3 web application that uses the collection_select helper with :multiple => true to handle a habtm relationship. This is working fine to set one or multiple values, however I have not figured out how to allow to REMOVE all selections.
Code:
<%= f.collection_select :category_ids, Category.find(:all), :id, :name,
{ :selected => #entry.category_ids },
{ :multiple => true, :name => 'entry[category_ids][]' }
%>
Once the user has ever set a category for an entry, how would I go about allowing it to be removed, so that this entry has no category? Is this possible with collection_select or would I need to add a checkbox to handle this specially?
P.S: I already tried with :prompt, :include_blank and :allow_blank, but as far as I could see neither of them did anything.
In your controller's update action, put in the following line:
params[:entry][:category_ids] ||= []
before the call to Entry.find.

Resources