How do you replace a collection_select (with :multiple => true) with a list of check_box options, such that there is a check_box option for each object in the collection?
Is there an elegant way to implement this using form builders (i.e. without using *_tag helpers)? I'd like to lean on ActiveRecord's built in functionality as much as possible...
I don't think there's a built-in "elegant" way to do this.
This railscast should get you going, though:
http://railscasts.com/episodes/17-habtm-checkboxes
http://asciicasts.com/episodes/17-habtm-checkboxes (text versionn)
You can do it like this (example using HAML).
%fieldset
Colors I like
- %w[red blue].each do |color|
= f.label color
= f.check_box :colors_liked, {multiple: true}, color, nil
The nil option at the end prevents Rails from building a hidden checkbox by the same name with a 0 value, which you definitely don't want if you're going for multiple selection.
This produces:
<label for="colors_liked_red">Red</label>
<input id="my_form_colors_liked_red" \
name="my_form[colors_liked][]" type="checkbox" value="red">
<label for="colors_liked_blue">Blue</label>
<input id="my_form_colors_liked_blue" \
name="my_form[colors_liked][]" type="checkbox" value="blue">
When the form is submitted, the parameters will contain an array of the values of the checked options.
Related
I feel like I'm going insane. This seems so simple but somehow I cannot find an answer on the internet.
I have a form with a checkbox in it. If the checkbox is checked, I want to display an input field. How do I do that in Ruby on Rails without Javascript? All the solutions I saw were with Javascript for like 9 years old.
= simple_form_for(#register) do |f|
.form-inputs
= f.input :first_name
= f.input :last_name, required: true
= f.check_box :special_request
= f.label :special_request
# and now here should be the if-else statement that should go something like this
if :special_request.checked?
= f.input :request
But I have yet to find out how exactly this is supposed to be implemented. It seems like the easiest task, but I have yet to find an answer. Also, no matter if the checkbox is checked or not, the value is always 1 when I look at it in with inspect.
The closest you'll get to this is the "Checkbox Hack"
.control-me {
display: none;
}
#toggle:checked ~ .control-me {
display: block;
}
<input type="checkbox" id="toggle">
<div class="control-me">
<input type="text">
</div>
Adapted from:
https://css-tricks.com/the-checkbox-hack/
Perhaps I'm being silly but I've been trying to set a default value for an input with ruby on rails for hours and haven't cracked it.
I'm making a partial which can either allow users to create new records but will also show existing records if they exist. Code as follows
<input type="text" <%= (#prices.empty? || #prices.first.name.length == 0) ? 'placeholder="General admission"' : "value=" + #prices.first.name.to_s %> >
Which works perfectly for any value that exists UNLESS there is a space, for example, if price.name = "general admission" OR if price.name = "" (in which case it prints the placeholder) I get the following produced
<input type="text" admission'="" value="'general" id="event_price_name" name="[event][price][0][name]" aria-label="..." class="form-control">
It seems to get tripped up by the space.
Am I trying to use Rails in a way it wasn't designed to be done in? I'm more used to PHP which may be it!
Thanks
You should use the text_field_tag helper to build the input, this is preferred over building it with partial interpolation. Also, placeholder will automatically be overwritten if there is a value so you don't need to handle that part in the code, that's how it behaves by default on the browser.
<%= text_field_tag :admission, #prices.first.name.to_s, {placeholder: 'General Admission'} %>
I have an association where a Vote has many Images, and I'd like this association to be checkboxes but instead of text have an image next to the checkbox instead of a text value the an example of the outputted HTML would look like:
<input class="check_boxes optional" id="vote_image_id_1" name="vote[image_id][]" type="checkbox" value="1"><img src="boat.png" alt="Big Boat"> <br />
Is this possible to do with simple form or will I need to write a custom field for this? If i have to write a custom field could someone recommend some good resources on making custom fields with simple form.
I can't test it right now, but I would do it more or less like this:
options = []
images.each do |image|
options << [image.id, image_tag(image.path)]
end
f.collection_check_boxes :votes, options, :first, :last
Maybe you can extract the construction of the options array to a helper method.
I hope it helps :)
I made a rails application, and I used datagrid gem to handle filters, pagination,and orders(ascending and descending). I was supposed to write <%= f.datagrid_filter filter%> to filter according to a filed of the table(Ex:title field in topics table).
Now <%= f.datagrid_filter filter%> returns a traditional html input tag like below. <input id="topic_report_title" class="title string_filter" type="text" size="30" name="topic_report[title]"> in the html console.
Now I want to put placeholder in that helper method only.
Can anybody help please?
Have you tried to do the following: <%= f.datagrid_filter filter, :placeholder => "placeholder text"%>
I have a settings model with a column options, and set it to serialize with serialize :options. In my view, I have a multiple selection box, using select("settings", "options", ['option1','option2','option3'], {}, :multiple => true) which works fine so long as the user selects at least one option. However, if they don't select any options, no options are submitted and so the options aren't updated.
How do I allow the user to select zero options from a multiple selection box in rails?
That has nothing to do with rails: html form won't send such parameter to server if nothing is chosen in 'select' element. But you should be able to fix it in controller. Something like this
if params[:settings] == nil
params[:settings] = [];
end
Not sure if there's more elegant solution.
Add a hidden field after the select box, that posts an empty value to "settings[options]"
It's same trick that rails uses to make sure unchecked checkboxes get posted as false.
I do not like assuming a value is empty if the attribute is not posted. It breaks the way Rails expects to update attributes, and it can present problems if you are using your controller actions also for APIs as well as HTML. My preferred way of handling this is by adding a hidden input field before multiselects.
<input type="hidden" value="" name="parent_model[my_attribute_ids][]">
If you use JQuery you can automate the addition of these hidden input fields:
$('select[multiple="multiple"]').each(function(i){
$(this).before('<input type="hidden" name="'+this.name+'" value="" />')
});
I realize this answer is not very timely, but I hope this will help someone with a similar question.
You could provide a "None" option.
Example from: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'}, {:multiple => true})
Would become
<select name="post[person_id]" multiple="multiple">
<option value="">None</option>
<option value="1">David</option>
<option value="2" selected="selected">Sam</option>
<option value="3">Tobias</option>
</select>