How do I enable multiple selection for option_form_collection? - ruby-on-rails

I can't figure out how to enable multiple selection for a input in Ransack.
<%= search_form_for #q,url: search_table_path do |f| %>
<%= f.label :country_code_eq %>
<%= f.select :country_code_eq,
options_from_collection_for_select(Country.all, :code, :name),
{prompt: 'Select a Country',multiple: true,include_blank: false}%>
<%= f.label :date_start %>
<%= f.text_field :date_start %>
<%= f.submit %>
<% end %>
The multiple: true does not work as I expected. It only show a normal dropdown box not a multiple selection box.
My first question is how to enable multiple selection ?
And my Second question is how do I keep the selected value and show it after the page loaded in selection box ?

I found the answer
<%= f.select :country_code_in, Country.all.map {|country| [country.name,country.code] }, {include_blank: 'All'}, {multiple: true} %>

Related

Collection_Select Does Not Respond to Input_HTML: {Multiple: True}

I am attempting to create a feature where users can add an existing record, recipe, to a collection of records, menu. I am using collection_select with a simple_form to allow users to select multiple records from a list however, the form is not responding to the input_html: { multiple: true } option, which should allow users to select multiple values. The form is as below, please let me know if any other code would be helpful for context.
Form:
<%= simple_form_for #menu, local: true do |f| %>
<%= f.label :title, :class => "form-component-header" %>
<%= f.text_field :title, :class => "form-field" %>
<%= f.label :recipe_ids %>
<%= f.collection_select :recipe_ids, f.object.user.recipes, :id, :title, input_html: { multiple: true } %>
<%= f.submit :class => "form_button" %>
<% end %>
.permit(....recipe_ids: [])
You need to update the permitted parameters in your controller. Now that you are sending multiple selections the parameter needs to be marked as expecting an array.

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

Rails option_select helper label

Using a bootstrap and rails along with datatables but keep getting a blank option on the datatable instead of a List helper that displays a topic label in the options list.
<%= f.label :activity, 'Select Activity' %><br />
<%= f.select :activity, options_for_select([
['#', 'Subject'],
['Math', 'Math'],
['Science', 'Science'],
['English', 'English']
]) %>
Here's how it looks on the filter page listing all postings should have the subject field displayed instead of blank space:
Thanks for your help,
RS
Rails's select accepts an argument for the empty value, so you can change your code to be the following (Since I don't think you want the Subject to be selectable):
<%= f.label :activity, 'Select Activity' %><br />
<%= f.select :activity, options_for_select([
['Math', 'Math'],
['Science', 'Science'],
['English', 'English']
]), include_blank: "Subject" %>
You can also improve your code by doing the following:
<%= f.label :activity, 'Select Activity' %><br />
<% activities = %w(Math Science English) %>
<%= f.select :activity, options_for_select(activities.zip(activities)), include_blank: "Subject") %>
option_for_select give you as option to select a value, you can select a value either from collection or can give your own.
In your case
<%= f.label :activity, 'Select Activity' %><br />
<%= f.select :activity, options_for_select([
['#', 'Subject'],
['Math', 'Math'],
['Science', 'Science'],
['English', 'English']
] , selected: "Math" ) %>
Hope this help you

Changing f.select to checkbox

I have the following in my view at which the can select several categories:
<%= form_for(#survey) do |f| %>
Categories <br>
<%= f.select :category_ids, Category.all.collect {|x| [x.name, x.id]}, {}, :multiple => true %><br>
<%= f.submit %>
<% end %>
I want the user to be selecting the categories with checkboxes instead of drop down list.
I am not sure how this is possible. Two parts: 1. How it will display several checkboxes and 2. How it will be saving the user's selections as it is saving fine for the f.select above.
The approach (which is not complete) that I though of is to iterate through the categories and add a checkbox for each category. However I am not sure if this will make sure that the several selections will be saved.
<% #categories.each do |category| %>
<%= category.name %><br>
<%= f.check_box :category_ids %>
Any guidance/tip to the right direction is greatly appreciated.
You can use collection_check_boxes
<%= f.collection_check_boxes :category_ids, Category.all, :id, :name, {}, :multiple => true %><br>

Using select_month in form_for

I am working in Ruby on Rails and I cant figure out how to use select_month in a form_for. What I am trying is:
<%= form_for(#farm) do |f| %>
<div class="field">
<%= f.label :harvest_start %><br />
<%= select_month(Date.today, :field_name => 'harvest_start') %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
which is outputting
["harvest_start", nil]
In order to use select month consistently with the rest of your form builder, use date_select without the day or month:
date_select(object_name, method, options = {}, html_options = {})
Just use f.date_select :harvest_start, {order: [:month]} i.e. add order: [:month] or discard_year: true, discard_day: true to the options hash, as specified in the docs
The benefit of this you can pass in all the remaining options as you would in the options hash. Something like:
<%= f.date_select :birth_date, {prompt: true, order: [:month]}, class: 'form-control' %>
(this was answered in comments by T. Weston Kendall so just making it a proper answer)
the following code is what i got to work.
_form.html.erb
<div class="field">
<%= f.label :harvest_start %><br />
<%= f.collection_select :harvest_start, Farm::MONTHS, :to_s, :to_s, :include_blank => true %>
</div>
farms_controller.rb
#farm.harvest_start = params[:harvest_start]
i also got select_month to work but i needed the :include_blank and i didn't want to spend the time figuring out what to do with the nil in an array.

Resources