How to delete the last element from ruby association input - ruby-on-rails

How to delete the last element from ruby association input
this is my input form field, which select all sizes and lists
= simple_nested_form_for [:admin, #product], wrapper: :horizontal_small_form do |f|
= f.association :standard_sizes, as: :check_boxes
This is what iam getting when iam inspecting it.
How to remove last option (custom size) from this.
<label class="check_boxes optional control-label">Standard sizes</label><span class="checkbox"><label for="product_standard_size_ids_1">
<input class="check_boxes optional" type="checkbox" value="1" name="product[standard_size_ids][]" id="product_standard_size_ids_1">S</label></span><span class="checkbox"><label for="product_standard_size_ids_2">
<input class="check_boxes optional" type="checkbox" value="2" name="product[standard_size_ids][]" id="product_standard_size_ids_2">M</label></span><span class="checkbox"><label for="product_standard_size_ids_3">
<input class="check_boxes optional" type="checkbox" value="3" name="product[standard_size_ids][]" id="product_standard_size_ids_3">L</label></span><span class="checkbox"><label for="product_standard_size_ids_4">
<input class="check_boxes optional" type="checkbox" value="4" name="product[standard_size_ids][]" id="product_standard_size_ids_4">XL</label></span><span class="checkbox"><label for="product_standard_size_ids_5">
<input class="check_boxes optional" type="checkbox" value="10" name="product[standard_size_ids][]" id="product_standard_size_ids_10">Custom Size</label></span><input type="hidden" name="product[standard_size_ids][]" value=""></div>

You can select the collection you wish to show with:
#rails 4
f.association :standard_sizes, collection: StandardSize.where.not(id: 10)
#rails 3
f.association :standard_sizes, collection: StandardSize.where('standard_sizes.id != 10')
But you should probably wonder why you need the field Custom Size in your database...

Related

Check_box and radio_button for form_helper in rails

I went through the rails documentation of rails, and could not find anywhere that gave me information about check_box or radio_button to deal with the model objects.
= f.radio_button(:recurring_status, true)
= f.label :recurring_status, "Yes?"
= f.radio_button(:recurring_status, false)
= f.label :recurring_status_false, "No?"
I tried this with the radio_button, but the value is just not passing from the form in the params. Same case with the check_box.
Can someone please explain to me why this is happening, and also why rails has not specified the uses of check_box and radio_button with Model Objects.
Also,
<%= check_box_tag(:pet_dog) %>
<%= label_tag(:pet_dog, "I own a dog") %>
<%= check_box_tag(:pet_cat) %>
<%= label_tag(:pet_cat, "I own a cat") %>
<input id="pet_dog" name="pet_dog" type="checkbox" value="1" />
<label for="pet_dog">I own a dog</label>
<input id="pet_cat" name="pet_cat" type="checkbox" value="1" />
<label for="pet_cat">I own a cat</label>
This is the example given in the offical documentation, both the checkbox have the same value as '1'. It's quite hard to understand what is happening here.
https://api.rubyonrails.org/v5.1.7/classes/ActionView/Helpers/FormOptionsHelper.html check this out.
collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial)
=> <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
<label for="post_author_ids_1">D. Heinemeier Hansson</label>
<input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
<label for="post_author_ids_2">D. Thomas</label>
<input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
<label for="post_author_ids_3">M. Clark</label>
<input name="post[author_ids][]" type="hidden" value="" />
collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial)
=> <input id="post_author_id_1" name="post[author_id]" type="radio" value="1" checked="checked" />
<label for="post_author_id_1">D. Heinemeier Hansson</label>
<input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
<label for="post_author_id_2">D. Thomas</label>
<input id="post_author_id_3" name="post[author_id]" type="radio" value="3" />
<label for="post_author_id_3">M. Clark</label>
collection_select(:post, :category_id, Category.all, :id, :name, {disabled: -> (category) { category.archived? }})
=> <select name="post[category_id]" id="post_category_id">
<option value="1" disabled="disabled">2008 stuff</option>
<option value="2" disabled="disabled">Christmas</option>
<option value="3">Jokes</option>`enter code here`
<option value="4">Poems</option>
</select>

Customize simple form radio buttons to match the theme markup

I’m using simple forms. The following line is what I have
=f.input :live_mode, as: :radio_buttons, label: false
which produces the following markup
<div class="form-group radio_buttons optional facility_live_mode"> .
<input type="hidden" name="facility[live_mode]" value="">
<label class="radio">
<label for="facility_live_mode_true">
<input class="radio_buttons optional" type="radio" value="true" name="facility[live_mode]" id="facility_live_mode_true">
Yes
</label>
</label>
<label class="radio">
<label for="facility_live_mode_false">
<input class="radio_buttons optional" readonly="readonly" type="radio" value="false" checked="checked" name="facility[live_mode]" id="facility_live_mode_false">
No
</label>
</label>
</div>
The wrapper label is the result of setting config.item_wrapper_tag in simple_form.rb to :label which matches the markup of the custom theme I’m using.
What I want to achieve is this
<input type="hidden" name="facility[live_mode]" value="">
<label class="custom-control custom-radio custom-control-inline">
<input class="custom-control-input" type="radio" value="true" name="facility[live_mode]" id="facility_live_mode_true">
<span class="custom-control-label">Yes</span>
</label>
<label class="custom-control custom-radio custom-control-inline">
<input class="custom-control-input" readonly="readonly" type="radio" value="false" checked="checked" name="facility[live_mode]" id="facility_live_mode_false">
<span class="custom-control-label">No</span>
</label>
but I have no idea how to go about doing this. Simple form config file provides a few options for customization but nothing that can produce the output that I need.
Is there something I can do to make this happen?

Rails: Simple Form checkbox and material design

I have been trying to figure out how to make simple form generate the correct code for a checkbox.
The code im trying to create is:
<div class="checkbox m-b-15">
<input value="0" type="hidden" name="group[open]">
<label>
<input class="boolean optional" type="checkbox" value="1" checked="checked" name="group[open]" id="group_open">
<i class="input-helper"></i>
Label text
</label>
</div>
So far im managed to generate
<div class="checkbox"><input value="0" type="hidden" name="group[open]">
<label class="boolean optional" for="group_open">
<input class="boolean optional" type="checkbox" value="1" checked="checked" name="group[open]" id="group_open">
Label text
</label>
</div>
With the following code
b.wrapper tag: :label do |ba|
ba.use :input
# <i class="input-helper"></i> tag should go here
ba.use :label_text
end
So how do I insert then i tag?
<i class="input-helper"></i>

Selecting multiple checkboxes with Capybara, weird behaviour

I am making an acceptance test on Ruby on Rails 3.0.11 with Capybara 1.1.2, and trying to select two checkboxes in a form.
Here is the view code for the checkboxes (using haml 3.1.3 and simple_form 2.0.4):
= f.input :countries,
:collection => MusicTeaser.teaser_countries,
:as => :check_boxes
The collection looks like this:
[["AT", "at"], ["CH", "ch"], ["DE", "de"], ["ZA", "za"]]
And the HTML generated:
<div class="input check_boxes optional">
<label class="check_boxes optional control-label">Countries</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_at" name="music_teaser[countries][]" type="checkbox" value="at">AT
</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_ch" name="music_teaser[countries][]" type="checkbox" value="ch">CH
</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_de" name="music_teaser[countries][]" type="checkbox" value="de" checked="">DE
</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_za" name="music_teaser[countries][]" type="checkbox" value="za">ZA
</label>
<input name="music_teaser[countries][]" type="hidden" value="">
</div>
The code to select them looks like this (pretty straightforward):
check 'music_teaser_countries_de'
check 'music_teaser_countries_za'
I expected that, on submit, the countries param would look like this:
params[:music_teaser][:countries] = ["de", "za"]
But it appears as this:
params[:music_teaser][:countries] = ["deza"]
So it seems like Capybara is doing something wrong when selecting more than one checkbox. Ideas?

Checking checkboxes with Capybara

Using Capybara I cannot for the life of me select a checkbox on my form.
In my request spec I've tried:
check("First Name")
page.check("First Name")
page.check("pickem_option_ids_10")
find(:css, "#pickem_option_ids_11[value='11']").set(true)
find(:css, "#pickem_option_ids_11").set(true)
Snippet from my form:
<div class="control-group check_boxes optional">
<label class="check_boxes optional control-label">Options:</label>
<div class="controls">
<label class="checkbox">
<input class="check_boxes optional" id="pickem_option_ids_10" name="pickem[option_ids][]" type="checkbox" value="10" />First Name
</label>
<label class="checkbox">
<input class="check_boxes optional" id="pickem_option_ids_11" name="pickem[option_ids][]" type="checkbox" value="11" />Middle Name
</label>
</div>
</div>
I got some of the find() ideas from this SO thread.
I've had some success in other specs where I have a single checkbox with a label of Active and I just say check("Active").
Had the same problem today, I looked around and this seemed to work:
find(:xpath, "//input[#value='10']").set(true)
of course you can replace '10' with anything you want - just check your HTML and use your value.
Hope that helps.
Capybara can't find checkbox "First Name" because your html is wrong. Your html should look like
<label class="checkbox" for="pickem_option_ids_10">First Name</label>
<input class="check_boxes optional" id="pickem_option_ids_10" name="pickem[option_ids][]" type="checkbox" value="10" />
In your view code
= label_tag "pickem_option_ids_10", "First Name"
= check_box_tag "pickem_option_ids_10", 10
Then check("First Name") should work.
Otherwise you can find("#pickem_option_ids_10").check

Resources