Ruby On Rails: Storing collection_select values into an array - ruby-on-rails

I have a form that has multiple collect_select fields and I want to be able to store the selected values into an array or hash.
So for example:
<%= f.collection_select(:package_ids, Package.all, :id, :concatTitle, {:prompt => true}) %>
<%= f.collection_select(:package_ids, Package.all, :id, :concatTitle, {:prompt => true}) %>
<%= f.collection_select(:package_ids, Package.all, :id, :concatTitle, {:prompt => true}) %>
I want all the selected id's to be stored into the same field :package_ids. What would be the correct syntax to do this?

Related

Submitting multiple attributes to controller from collection select in form

I want a collection select tag in rails to submit two attributes of the chosen item to the controller when the form is submitted. Basically, I have a list of counties and I want to submit both the county and the state as parameters. No problem having it submit one or the other, but not both. Am I thinking about this the wrong way? Here's what I have so far...
<%= form_tag(plans_path, method: 'get', action: 'screen2') do %>
<%= text_field_tag :ZIP, "ZIP Code", id: "zipBlur"%>
<%= collection_select(nil, :county, #counties.order('RES_RATIO DESC'), :COUNTY, :COUNTY_NAME, {:selected => "#{params[:county]}"}) %>
<%= submit_tag 'Screen', :name=> nil %>
<% end %>
Thanks for your help!
using :multiple => true
ex:
<%= collection_select(:ingredient, :supplier_ids,
Supplier.all(:order=>"name ASC"),
:id, :name, {:selected => #ingredient.supplier_ids, :include_blank => true}, {:multiple => true}) %>

Rails 5.1 how to not list nulls in a collection_select?

I have a collection_select field listed here:
<%= form.collection_select :parent_id, Document.order(:name), :id, :name,
{:include_blank => ''}, {:class => 'form-control'} %>
The name field has nulls in it for certain cases. I only want to list the name in the collection_select if it is not null.
Is there a way to do this?
Set up a scope in your Document class:
##document.rb
scope :named_documents, -> { where.not(name: nil).order(:name) }
Then you can use it like this:
<%= form.collection_select :parent_id, Document.named_documents, :id, :name, {:include_blank => ''}, {:class => 'form-control'} %>
you can do
<%= form.collection_select :parent_id, Document.named_documents.reject{|d| d.name.nil?}.order(:name), :id, :name,
{:include_blank => ''}, {:class => 'form-control'} %>
I updated it.

Rails 3: class not recognized for form collection?

I have a nested form with the following field:
<%= f.label :size %><br />
<%= f.collection_select :size, Video::SIZE, :to_s, :to_s, :include_blank => true, :class => "sizefield" %>
As you can see, I've assigned the class, "sizefield", to this field, but for some reason, my app is not recognizing it. What am I doing wrong here?
Rails functions that allow two terminal hashes get confused if you don't specify the boundaries of those hashes exactly. For example:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
You're not separating the options from the html_options. Try this:
<%= f.collection_select :size, Video::SIZE, :to_s, :to_s, {:include_blank => true}, {:class => "sizefield"} %>

Not able to Pass customer_id Value in the form to a partial as hidden field value in ruby on rails

I have this in the Main form
<%= simple_nested_form_for #customer_bill do |f| %>
<%= f.label :customer_id %>
<%= f.collection_select :customer_id, Customer.all,:id,:name, {:prompt => "Select Customer"}, :style => 'width:205px;' %><br />
<%= f.link_to_add "Add", :customer_bill_line_items, :locals => {:text_1 => :customer_id} %>
/* rest of code */
<%end%>
And i have this in my customer_bill_line_items Partial
<%= f.hidden_field :customer_id, :value => :text_1 %>
/*rest of code*/
But i am not Able to capture the selected customer id in the partial. The value of hidden field is coming as 0. Any guidance on how i can solve this matter will be great. Thanks in advance
<%= f.hidden_field :customer_id, :value => :text_1 %>
I am guessing that customer_id in an integer field, and you are setting the :value as text_1 which is a string.
if the value of text_1 is an integer then text_1.to_i

Default value for select field from query string

I have this link in my code:
link_to "New question", new_question_url(:category_id => #category.id)
I have this code in my new question form:
<p>
<%= f.label :category_id %><br />
<%= f.collection_select :category_id, Category.all, :id, :name %>
</p>
How can I make Rails automatically select the category from the category_id querystring item, so it is the default one in the collection_select?
Thanks.
Your need to pass in a reference to the current object like this:
<%= f.collection_select(:your_object, :category_id, Category.all, :id, :name) %>
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:prompt => true})
"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."
See here:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

Resources