Rails: Multiple dropdown menus collection_select - ruby-on-rails

Super Rails n00b here: Currently I have a form with the following code:
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
and it currently works how I want it to but now I would like to have multiple drop down menus so I can select multiple accounts. I do not want the multiple select to be on the same dropdown.
If I do this:
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
only the last selection appears in the params. How can I make it so the params would look like this:
"journal"=>{"account_ids"=>["1","2","3"]}
Can collection.select do this or should I be using something different? Any help would be greatly appreciated. Thanks!

You need to add one option :multiple :
<%= f.collection_select :account_ids, #accounts,
:id, :name, { include_blank: true },
{ multiple: true } %>
Note: :multiple- If set to true the selection will allow multiple choices.
I wrote a little snippet to test it. My code :
<%= form_for #track, url: fetch_path do |f| %>
<%= f.collection_select :label, #tracks, :id, :title, {include_blank: true}, {multiple: true} %>
<% end %>
Here is the page :
Or, if you really want to duplicate:
<% klass = f.object.class.model_name.param_key %>
<%= f.collection_select :account_ids, #accounts, :id, :name, { include_blank: true } , { name: "#{klass}[account_ids][]" } %>
Write the above line 3 times.

If your parameter name ends in "[]", then all inputs with that name will be collated into an array with that name.
So, your select tag (in html) will be like
<select name="account_ids[]"><option>...
and to make this using the collection_select helper, try
<%= f.collection_select :account_ids, #accounts, :id, :name, {include_blank: true}, {:name => 'account_ids[]'} %>

Related

PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type integer

I am getting this error in the Production site and this is the code in my views
<%= form_for [:admin, #course], :remote => true do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :duration %>
<%= f.number_field :duration, class: "input-md form-control mb-20"%>
<%= f.label :program_id %>
<%= f.collection_select :program_id, Program.where('id'), :id, :name, {}, {class: "input-md form-control mb-20" } %>
<%end%>
This works in my local server where i have sql db setup.
Program model
has_many :courses
can anyone guide me?
Where clause isn't calling anything to compare with, so PG doesn't know what to include in the results. A where clause must evaluate to true/false.
just replace
<%= f.collection_select :program_id, Program.where('id'), :id, :name, {}, {class: "input-md form-control mb-20" } %>
by
<%= f.collection_select :program_id, Program.all, :id, :name, {}, {class: "input-md form-control mb-20" } %>
If you have some problem with some of the Program in your data base, add a column as status in programs table and make changes here as
<%= f.collection_select :program_id, Program.where("status =?", true), :id, :name, {}, {class: "input-md form-control mb-20" } %>

Style Rails Select Tag

I have a form in rails with a select tag. I'm trying to style it like so:
<%= f.select :role, collection: User.roles.keys.to_a, class:"form-control form-control-lg" %>
I have tried the following:
<%= f.select :role, collection: User.roles.keys.to_a, {}, class:"form-control form-control-lg" %>
and
<%= f.select (:role, collection: User.roles.keys.to_a, class:"form-control form-control-lg") %>
But can't get the style to apply to the drop down.
Can you try the following
<%= f.select :role, options_for_select(User.roles.keys.to_a, params[:role]), {}, class: 'form-control form-control-lg' %>
I think will help

populate combo box for edit form in rails 4

I have the following form to edit
<%= form_for #post do |f| %>
<%= f.text_field :title %> #This shows correctly
<%= f.collection_select :product, Product.all, :id, :name %>
<% end %>
product is the column which will save the id(primary key) of Product table. How to show the saved value in my select box.
Just use :selected option
<%= f.collection_select :product, Product.all, :id, :name, :selected => #post.product %>
But this works
<%= f.collection_select :product, Product.all, :id, :name, :selected => #post.product.id %>

How to DRY up this conditional collection_select in form partial?

I have a form partial that is being called in a content_for :sidebar. This collection_select should have "selected" set if the page calling the partial is a specific package page. Otherwise, it should have a "prompt" to select. How would I DRY this up? I tried an inline ternary on a single collection_select to no avail.
<%- if #package.blank? -%>
<%= f.collection_select :package_name, Package.all, :name, :name, :prompt => "Please Select" %>
<%- else -%>
<%= f.collection_select :package_name, Package.all, :name, :name, :selected => #package.name %>
<%- end -%>
Thanks
How about:
<%= f.collection_select :package_name, Package.all, :name, :name,
#package.blank? ? { :prompt => "Please Select" } : { :selected => #package.name } %>

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