I want to save multiple check box category[] into database :-
<label><%= f.check_box :category,{class: 'chk'},1,0%>Apple</label>
<label><%= f.check_box :category,{class: 'chk'},1,0%>Orange</label>
<label><%= f.check_box :category,{class: 'chk'},1,0%>Banana</label>
Here my create form
Edit form
controller
database structure
There is a :multiple option, if that's what you need? It's hard to understand exactly what you want
check_box("puppy", "commands", {:multiple => true}, "sit", nil)
check_box("puppy", "commands", {:multiple => true}, "fetch", nil)
check_box("puppy", "commands", {:multiple => true}, "roll_over", nil)
Further examples here: https://apidock.com/rails/ActionView/Helpers/FormHelper/check_box
Related
I'm trying to write a select tag that makes use of a helper I've written.
When I write the tag like this, it all works.
<%= select_tag "roles", options_from_collection_for_select(#roles, "id", "name"), :multiple => true, :class => 'chosen-select form-control' %>
I want to use a helper to present the name of the role differently to the name in the database. I made a helper to do that. Now I'm trying to use that helper method instead of the :name attribute.
<%= select_tag "roles", options_from_collection_for_select(#roles, "id", "<%= text_for_role(name)%>"), :multiple => true, :class => 'chosen-select form-control' %>
The above attempt doesn't work. I can't find an example of how to use a helper inside a select tag. Is it possible?
If you want to interpret ruby within a string you need to use #{} such as
<%= select_tag "roles", options_from_collection_for_select(#roles, "id", "<%= #{text_for_role(name)}%>"), :multiple => true, :class => 'chosen-select form-control' %>
You could also directly put your ruby code instead of interpolating:
<%= select_tag "roles", options_from_collection_for_select(#roles, "id", text_for_role("name")), :multiple => true, :class => 'chosen-select form-control' %>
The above will just solve the error you encountered but i think it doesn't address the issue in the end. If you would like to display a different name than the one in the database, i guess you would need to define text_for_role in the Role model.
and then you call:
<%= select_tag "roles", options_from_collection_for_select(#roles, :id, :text_for_role), :multiple => true, :class => 'chosen-select form-control' %>
This will call the method text for role for each AR object to set the name attribute in html tag.
Ok, I have a multi select box controlled by chosen jquery plugin.
I can get multi select working without ajax, and ajax working without multiselect, but not the two together.
Here multi select works, but reloads the whole page when an item is selected (remote not working)
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {}, { :multiple => true, remote: true, onchange: "this.form.submit();" } %>
Here ajax works fine, reloading my list only, but I can only select one option at a time (multiple not working)
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {},:data => { :multiple => true, :remote => true, onchange: "this.form.submit();" }} %>
I want to be able to multi select, and with each new addition, to send an ajax request and reload the list.
Any advice on linking everything would be great! Thanks!
Figured it out:
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {},:data => { :remote => true, onchange: "this.form.submit();" }, :multiple => true %>
My code:
<%= f.select :area, options_for_select([['a','a'],['b','b'],['c','c']]), {}, {:class => 'span3 controls controls-row'}, :selected => params[:area] %>
The result is:
ArgumentError in Users#edit
Showing /home/airson/rails_projects/friends_of_local/app/views/users/edit.html.erb where line #17 raised:
wrong number of arguments (5 for 4)
Why am I getting this error?
No need to use :selected just pass your params[:area] alone to options_for_select as a second argument:
<%= f.select :area,
options_for_select([['a','a'],['b','b'],['c','c']], params[:area]),
{}, { :class => 'span3 controls controls-row' } %>
The last value of your params[:area] will be selected.
Hope it helps ; )
You should pass :selected option to options_for_select method, like this:
<%= f.select :area, options_for_select([['a','a'],['b','b'],['c','c']], :selected => params[:area]), {}, { :class => 'span3 controls controls-row' } %>
The above answers didn't work for me on Rails 6.
This is what worked. Copied from one of the answers on Reddit
= f.select(:results, options_for_select(['Accepted', 'Not Accepted', 'Rejected', 'Acc', 'Rej', 'Information Only', 'N/A'], selected: #report.results || nil), { include_blank: "Select Result" }, { class: 'form-control select2', style: 'width: 100%;'})
Below is what worked for me using f.select with custom names and values including a blank field while using the Ransack search. Thanks to mayorsanmayor's example for getting me started in the right direction. This also works for rails 5.1.7.
<%= f.select(:status_eq, options_for_select([ ["Verified", "verified"], ["Non Verified","notVerified"]], selected: #q.status_eq || nil), {:include_blank => "Select Status"}, {class: 'form-control'}) %>
I can't figure out the right code for using a predetermined set of options for a multi-select field. I want to have a list of skills in a drop down that users can select from. Here is the code I am using, it works fine as a single select field, but not as a multi-select:
<%= form_for(#user, :html => { :class => "form-stacked" } ) do |f| %>
...
<div class="clearfix"><%= f.select :skill_list, options_for_select(["Asst", "dir", "pres"]),
{
:multiple => true,
:class => "chzn-select",
:style => "width:450px;" } %></div>
...
<% end %>
Anyone have any suggestions? Eventually, I will want to store all of the options for the multi-select form elsewhere because there will be a bunch, but this is the first challenge I can't figure out..
Thanks.
EDIT
I have also tried:
:html => { :multiple => true, :class => "chzn-select", :style => "width:450px;" } and it doesnt work either
There needs to be two pairs of brackets, one for the options, and one for html_options, like so:
<%= f.select :skills_list, options_for_select(["Asst", "dir", "pres"]), {}, {:multiple => true, :class => "chzn-select", :style => "width:450px;" } %>
See the docs for the select helper.
I've built a multi-select form (from within form_for) like this:
<div class="rounded-block quarter-wide radio-group">
<h4>Exclude customers from source:</h4>
<%= f.select :excluded_sources, options_for_select(User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}), {:include_blank => false}, {:multiple => true} %>
<%= f.error_message_on :excluded_sources %>
</div>
this works well for what I need. The only problem is that when i go back to the page that displays the choices, I don't see what was previously selected (i.e. what is already in the DB at time of rendering). Is there an easy way to get rails to display what's previously been selected? I'd MUCH prefer not to switch to check boxes.
in my matching profiles model (corresponding to the table that stores excluded_sources), i have this:
serialize :excluded_sources
this ended up being the relevant piece:
:selected => matching_profile.send(:excluded_sources)
here:
<div class="rounded-block quarter-wide radio-group">
<h4>Exclude customers from source:</h4>
<%= f.select :excluded_sources, options_for_select(User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}, :selected => matching_profile.send(:excluded_sources)), {:include_blank => false}, {:multiple => true} %>
<%= f.error_message_on :excluded_sources %>