Changing f.select to checkbox - ruby-on-rails

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>

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.

Rails 4 form: display database value in select field

There are similar questions on Stack Overflow, like this one or that one, and I tried what the answers recommended, but I could not fix my problem.
I have a Rails form — NOT a simple_form form — with the following field in my new view:
<div class="field">
<%= f.label :price, "Hourly Rate ($)" %><%= f.select :price, ['10', '20', '30', '40', '50', '60', '70', '80', '90', '100'] %>
</div>
Then, I want to re-display this form in my edit view, with the value that was saved to the database, so I tried:
<div class="field">
<%= f.label :price, "Hourly Rate ($)" %><%= f.select :price, ['10', '20', '30', '40', '50', '60', '70', '80', '90', '100'], :selected => current_user.price %>
</div>
The problem is that the select field displays the first value instead of the one I am expecting.
—————
UPDATE: Following the answers given below, I implemented the following code:
<div class="field">
<%= f.label :price, "Hourly Rate ($)" %>
<%= f.select :price, [['10', '10'],['20','20'],['30','30'],['40','40'],['50','50'],['60','60'],['70','70'],['80','80'],['90','90'],['100','100']], selected: current_user.price, include_blank: 'Select your price' %>
</div>
It does not seem to work. Unless I made a mistake?
—————
Any idea how to make this work?
You don't need two separate forms.
in new.html.erb and edit.html.erb put the following:
<%= render 'hourly_rate' %>
then in _hourly_rate.html.erb put the following:
<div class="field">
<%= f.label :price, "Hourly Rate ($)" %>
<%= f.select :price, options_for_select([['10',1], ['20',2], ['30',3], ['40',4], ['50',5], ['60',6], ['70',7], ['80',8], ['90',9], ['100',10]], #your_obj.price) %>
</div>
where #your_obj.price is something like 1, 2, 3...(or you can modify this to the actual prices if you'd prefer. You'd just need to change the arrays accordingly).
The options_for_select method is a helper whose second parameter allows you to pre-select an option by passing its value. For more on that, take a look at the docs.
try this:
<div class="field">
<%= f.label :price, "Hourly Rate ($)" %><%= f.select :price, [['10', '10'],['20','20'],['30','30'],['40','40'],['50','50'],['60','60'],['70','70'],['80','80'],['90','90'],['100','100']], selected: current_user.price, include_blank: 'Select your price' %>
</div>
you should pass an array of arrays which the left cell is the name and the right one is the value, then pass in selected by value and not by name, you should use this also in the first form:
<div class="field">
<%= f.label :price, "Hourly Rate ($)" %><%= f.select :price, [['10', '10'],['20','20'],['30','30'],['40','40'],['50','50'],['60','60'],['70','70'],['80','80'],['90','90'],['100','100']] %>
</div>

Displaying and selecting from a HABTM

Currently I have a Category and Post model, joined by a HABTM relationship.
Posts belong to multiple categories and have many attributes.
Categories just have a Name Property.
How do I create a multi-select form in my Posts _form.html.erb so I can select which categories I want the post to be assigned to?
<%= form_for #post do |f| %>
<div>
<%= f.label :category_ids, "Categories" %><br />
<%= f.collection_select :category_ids, Category.order(:name), :id, :name, {}, {multiple: true} %>
</div>
<div>
<%= f.submit 'Submit' %>
</div>
<% end %>
Try using select and collection. You may have to change the collection, not sure exactly what Category options you want to be able to choose from. Something like this:
<%= f.input :post, as: :select, collection: Category.posts, include_blank:false %>
Or checkout out the collection_select form helper method
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select

How do I enable multiple selection for option_form_collection?

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} %>

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