I'm trying to include a boolean value as a dropdown box using the following code in my edit view:
<div class="col-md-8">
<%= f.select :match, options_for_select([['On', true], ['Off', false]]), class: 'form-control input-md' %>
</div>
However I'm experiencing two problems:
1.It does not display the correct value. Even when the user's value is false, it still displays On.
2.It does not implement the styling. The inspector shows that it implements it as follows:
<div class="col-md-8">
<select name="user[match]" id="user_match">
<option value="true">On</option>
<option value="false">Off</option>
</select>
</div>
What am I doing wrong?
Do it like this:
<%= f.select :match, options_for_select([['On', true], ['Off', false]], selected: your_object.match),{}, {class: 'form-control input-md'} %>
Related
How do I assign the selected option value:
<select class="select2-simple-dropdown">
<% Season.all.each do |season| %>
<option id="chosen-season" value="<%= season.id %>"><%= season.name %></option>
<% end %>
</select>
To a form's field, let's say: Voyage.given_season ?
Use the rails select field instead and do it like this
<%= f.select :season_id, Season.all.pluck(:name, :id), {},
{ class: 'select2-simple-dropdown'} %>
Hope this helps.
If you want your select input to accept multiple options, you can pass multiple: true
<%= f.select(:season_id, Season.all.collect {|m| [ m.name, m.id] }, class: "form-control select2-simple-dropdown", id: "list-markets", multiple: true) %>
https://aalvarez.me/posts/select2-with-simple-form-in-rails/
The code illustrating the bug is from a Redmine plugin. A _form.html.erb partial contains this fields_for:
<%= f.fields_for :information do |information| %>
<p><%= information.text_field :middlename, :label => l(:label_people_middlename) %></p>
<p><%= f.text_field :lastname, :required => true %></p>
Then one of the fields declares a label:
<p><%= information.select :gender, Person.get_genders, :label => l(:label_people_gender)%></p>
That generates this HTML:
<p><label for="person[information_attributes]_gender">Gender</label>
<select name="person[information_attributes][gender]" id="person_information_attributes_gender">
<option selected="selected" value="0">Male</option>
<option value="1">Female</option>
</select></p>
The <label for=""> value has [] in it instead of underscores _, so it does not match the target field's id="person_information_attributes_gender". Clicking on the label does not put the keyboard focus into that <select> field.
Is this a known bug in Rails 4.2.8? Is there a fix or workaround available - besides just writing the <label> in raw HTML?
What happens if you try using an actual Rails label, instead of passing label as an option to the 'select'? (I'll be honest, I haven't actually seen the label option on a select like that, before). Something like this:
<p>
<%= information.label :gender, l(:label_people_gender) %>
<%= information.select :gender, Person.get_genders %>
</p>
I'm trying to create a Select element using Rails form_for helper. This is what I'm trying to create:
<label for="charge_occurrence">When to charge</label>
<select class="w-select" data-name="charge_occurrence" id="charge_occurrence" name="charge_occurrence" required="required">
<option value="monthly">Monthly</option>
<option value="episodic">Per episode</option>
</select>
I have tried several variations of this:
<%= f.select :charge_occurrence, [['Monthly', 'monthly'], ['Per Episode', 'episodic']], data: {name: 'charge_occurrence'}, html: {class: "w-input", maxlength: "256", required: "required"} %>
I can get everything to work except for the data-name part.
Try <%= f.select :charge_occurrence, [['Monthly', 'monthly'], ['Per Episode', 'episodic']], html: {'data-name' => 'charge_occurence', class: "w-input", maxlength: "256", required: "required"} %>
See: How to add data attribute in Rails form select tag?
I have a select option in my rails application:
<%= f.select :willingToRelocate, ['Yes', 'No'] %>
It works fine, now I'm trying to style it with bootstrap, but can't get style to work.
This is whats on the bootstrap guide:
<select class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
And heres a link to the bootstrap styling doc: http://getbootstrap.com/css/
You could style it like this <%= f.select :willingToRelocate, ['Yes', 'No'], {}, class: 'form-control'} %>
You could check out this link. The api.rubyonrails have a great amount of information that you could use for these kind of problems.
I'm not sure why, but my form is not showing the options selected on submit, even though the params hash shows that the information is being returned to the page.
Collection select code:
<%= f.collection_select :post_topic_ids, PostTopic.all, :id, :name, {}, { multiple: true, class: 'form-control' } %>
Which renders:
<select multiple="multiple" class="form-control" name="post[post_topic_ids][]" id="post_post_topic_ids">
<option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option>
</select>
Params returned after form validation error
params = {"post"=>{"post_topic_ids"=>["", "1"]}}
Update
I have also tried:
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }), multiple: true %>
and:
<%= select_tag 'post_topic_ids', options_from_collection_for_select(PostTopic.all, "id", "name"), multiple: true %>
Which renders:
<select name="post_topic_ids[]" id="post_topic_ids" multiple="multiple"><option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option></select>
you need to specify which element is selected a third parameter
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }, --->selected_element<---), multiple: true %>
look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select for some examples.