I've looked at How do I set the HTML options for collection_select in Rails? and I'm sure I'm missing something obvious, but I can't get this to work.
My select currently looks like:
<%= f.collection_select :broadcast_id, broadcasts, :id, :to_s,
:include_blank => 'Broadcast on...' %>
and I've tried including :class => 'prevent_collapse', which does nothing, as well as {:class => 'prevent_collapse'}, which gives me an error.
If anyone can point out how to do this, I'll be super grateful!
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
=>
f.collection_select :broadcast_id, broadcasts, :id, :to_s,
{:include_blank => 'Broadcast on...'}, {:class => 'prevent_collapse'}
And what error do you have?
And does broadcast item has got :to_s method? It will return class name, as I think.
Is that field :include_blank => {}, compulsory ? I tried with :include_blank => false and it worked. I wonder if we can avoid it ?
Related
I have a select with options filled in from a collection
<%= select('task', 'person_id', Person.where(:job_id => #job.id).order(:name).collect {|p| [p.name, p.id]}, {:include_blank => true}, :required => true) %>
I'd like to add a 'Not Applicable' option to this select but am unsure how. I have it set to add a blank and I also have it set to required. With both of those true, someone can't just choose the blank. I have the required set because I want my staff to think about the option they select.
Thanks for the help!
I'd create a helper method something like this
def person_options(options = {})
options_for_select([["Not Applicable", ""]] + Person.where(:job_id => #job.id).order(:name).collect {|p| [p.name, p.id]}, options)
end
Then you can call it from erb like so
<td><%= f.select :person_id, person_options(selected: #person. person_id, include_blank: true), {}, {style: 'width:auto'} %></td>
might need to tweak for your project
I am trying to allow for multiple value selection from a collection in a Rails form. The field is working but does not allow for multiple selections (once an alternative option is selected the previously selected is unselected). I am using Bootstrap CDN, which I don't presume is causing issues, but I may be wrong?
Can you see anything wrong with this code?
<div class="field form-group row">
<%= f.label :industry_ids, class:"col-sm-3"%>
<%= f.collection_select(:industry_ids, Industry.all, :id, :name, {:multiple => true}, size: Industry.all.length) %>
</div>
Thanks for your help.
I believe your problem is that you're putting {:multiple => true} in the wrong options hash. The method signature for collection_select looks like this:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
multiple is an html attribute of the select tag itself (here's a doc), so you want to pass that to html_options, not options. Your code looks like this:
f.collection_select(:industry_ids, Industry.all, :id, :name, {:multiple => true}, size: Industry.all.length)
Where Industry.all is the collection, :id is the value_method, and :name is the text_method, which means { :multiple => true } is getting passed to options, not html_options. Move it to the second hash and you should be fine.
I'm trying to change the option that is selected on collection_select on my form in rails.
My code look like this:
<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, {}, {class: 'form-control m-b', :selected => #course_template.course_type.name } %>
However the option selected always shows the first one and never changes unless the user selects a different option.
The resulting html looks like this:
<select class="form-control m-b" selected="selected" name="course[course_type_id]" id="course_course_type_id">
<option value="1">Driving</option>
<option value="2">Pratical</option>
</select>
Any ideas on what I'm doing wrong?
It looks like you're putting the :selected key in the html_options argument attributes.
Here is the method definition for collection_select:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
Try this:
<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, {:selected => #course_template.course_type.name}, {class: 'form-control m-b' } %>
<%= f.collection_select :course_type_id, CourseType.where(:deleted => false), :id, :name, { :selected => #course_template.course_type.id }, {class: 'form-control m-b' } %>
The selected parameter takes the value, and not the name of the option.
From collection_select definition, selected option and html_options are different params.
For further understanding, refer here.
I have select
<%= f.select :visibility, collection_for_visibility_select, :include_blank => false %>
And a helper with values for select:
def collection_for_visibility_select
[
[l(:label_crm_contacts_visibility_project), Contact::VISIBILITY_PROJECT],
[l(:label_crm_contacts_visibility_public), Contact::VISIBILITY_PUBLIC],
[l(:label_crm_contacts_visibility_private), Contact::VISIBILITY_PRIVATE]
]
end
I want to add default select value to the select, and this is what I tried:
<%= f.select :visibility, collection_for_visibility_select, :selected => Contact::VISIBILITY_PUBLIC, :include_blank => false %>
it gave me default select value, but when i want to edit record and switch visibility to something else,i still got VISIBILITY_PUBLIC
How do I fix it?
You can try this:
<%= f.select :visibility, collection_for_visibility_select, :selected => (f.object.visibility.nil? ? Contact::VISIBILITY_PUBLIC : f.object.visibility), :include_blank => false %>
It will read the value from the model first, and if it is nil, will use the default value.
I have this piece of code, while using simple_form:
= simple_form_for :report do |f|
= f.association :presets,
:collection => #account.presets.collect{ |p| [p.name, p.id] },
:as => :check_boxes
How can I preselect a specific preset checkbox, knowing that the ID of this preset is passed within params[:preset_id]? The checkboxes' HTML name attributes are report[preset_ids][].
According to the simple_form documentation:
The association helper just invokes
input under the hood, so all options
available to :select, :radio and
:check_boxes are also available to
association. Additionally, you can
specify the collection by hand, all
together with the prompt:
f.association :company, :collection
=> Company.active.all(:order => 'name'), :prompt => "Choose a Company"
So, you should use something like this:
= simple_form_for :report do |f|
= f.association :presets,
:collection => #account.presets.collect{ |p| [p.name, p.id] },
:as => :check_boxes,
:checked => params[:preset_id]
I don't have experience with simple_form, but this might help :)
An update for everybody. the :selected option did not work for me. I used:
:checked => [2, 3]
Hope it helps someone.
f.association Really did the trick, thanks :), for preselecting, saving, and everything, I don't have reputation enough to vote up your answer (#claudio-acciaresi) that's why I'm commenting here...
This is my snippet:
<%= f.association :association, collection: Model.all,
value_method: :id, label_method: :name,
as: :check_boxes, include_blank: false %>
Replace symbol :association with the current has_many from the model.
Replace Model.all, for your source data.
Hope it gets useful for someone else :)
Regards.
Don't forget to cast params[:preset_id] to integer:
params[:preset_id].to_i