Rails option_select helper label - ruby-on-rails

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

Related

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

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>

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>

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

Multiple Checkboxes, Convert to String, Single DB Column Rails

I have a form, that among other things, contains about 20 different checkboxes. Like so:
<%= form_for #inventory do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
...
<p>
<%= f.check_box :apple %><%= f.label :apple %><br />
<%= f.check_box :banana %><%= f.label :banana %><br />
<%= f.check_box :orange %><%= f.label :orange %>
...
</p>
...
<% end %>
What I want to do is take the value of the selected checkbox, comma delimit them, and save them in a column in the db. So if the apple and orange checkbox is checked it saves as:
#inventory.fruit = "apple, orange"
how do I do this?
I don't think we can send multiple values as a string rather than an array. Look at the below solution
In Rails, how to handle multiple checked checkboxes, just split on the , or?
The solution is in pure HTML code but you can use check_box_tag instead.

Resources