how to store an array from a multiple select - ruby-on-rails

i am currently using this select = select(:schedule, :selected_players, #players.map { |p| [full_name(p), p.id] }, {:include_blank => 'None'}, "data-placeholder" => 'Add Players to Lineup', :prompt => 'Add Players to Lineup', :multiple => "multiple")
and would like to store the information into an array within the database, then access this array for different pars of the site
there is a copy of things im needing to know
how best to store into the database, current this field value is binary
how to then extract each value from the array
thanks

Sounds like you want serialize.
If you make the selected_players column a text column and mark your Schedule model with
serialize :selected_players
Then you can save a Ruby array into the attribute. It'll be written to the database in YAML, and pop out exactly as it went in, as an array, when you read it.

Related

Rails Form multi-select list keyboard keys+display type and subtype

I have a form for #product that does multi-select list of product types(stored in database with has-many-through association between product-type.It works correctly by putting correct values in DB.
<%= f.collection_select(:type_ids, Type.all.order(:name), :id, :name,
{:selected => #product.type_ids, include_blank: false,:required => true},
{:multiple => true, size: 5})%>
But I have 2 issues:
I'm unable to use ctrl+click key for multi select. Only shift+click works. Any other method other than this or do I need to enable some keyboard/browser for Macbook Pro-version 10.12.2 & Chrome?
I have in the DB a column subtype(brand) for product. I want to display in the above a multi-select box showing both ptype and subtype columns from DB table Type as following: (I guess it's by grouped_options_for_select but can't find relevant example of retrieving from DB.)
Clothes -- M&S Clothes -- Next Clothes -- Monsoon
so chosen is what you are looking for :)

How to select the values dynamically from select box based on the previous select box?

I am sorry if i cannot make you understand.
I have two tables where the first table contains categories and the next one contains sub-categories and i have another table item where i have has_and_belongs_to_many relation with sub-category table. When i create a new item, i have to display the categories in the first select box and based on the selection, the sub-categories has to be displayed in another select box and the value has to be added. I have already another like shown below
<%= collection_select :item, :sub_category_ids, SubCategory.find(:all, :conditions => ["category_id = 7"], :order => 'sub_category_name ASC'), :id, :sub_category_name, { :selected => #item.sub_category_ids }, { :multiple => true, :name => 'item[sub_category_ids][]' } -%>
and now i want to add this one like
along with that. How can i do this?
You'd have to use AJAX to achieve what you want.
You would want to use Javascript to watch the first drop down value. When that value changes, you would want to make an ajax call to retrieve the values of the second drop down, then fill the second dropbox based on the AJAX response

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!

Multiple selection in RoR form_for

I have a list of items that I want to have as options for a variable. They will be saved in the model as an array, and are to be displayed as a list in the form_for. I was using
f.select(:var_name, [["option1"],["option2"],["option3"]], {}, {multiple: "multiple"})
Which works great to save into the model.
But when going back to the form, nothing is selected (even if the variable has them all saved). Then if I submit the form again, it passes an empty array. The only way for it to save correctly is to re-select the ones I want every time I view the form.
How can I get them to pass into the multi-select box?
I believe your problem stems from your choices parameter. You probably need an array of [option,id] mappings:
f.select(:person_id, Person.all.collect {|p| [ p.name, p.id ] }, {}, { :multiple => true })
When I started working on it again today, it was working. I'm not sure what change was made, but it could be that I needed to restart the server. It still looks like
f.select(:name, [[" "],["option"],["option2"],["option3"]], {}, {:multiple => true})
So it must not have been this code. In addition, the form beginning looks like
form_for(#model_name) do |f|
which hasn't changed either.
Regardless, it works now. Thanks!

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