Check_box and radio_button for form_helper in rails - ruby-on-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>

Related

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>

How to delete the last element from ruby association input

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...

ASP.Net MVC 4 C# login form with PhantomJS and Selenium

I have a specific question, as i can't figure out how to submit form without "id" or "name" in selenium.webdriver (phantomjs) there is html logim form:
<div class="ui-helper-child-2 ui-page-column-last ui-page-column-span-12">
<div class="ui-page-column-block ui-widget-signin">
<h1>Sign In</h1>
<form action="/glo/en-us/session,create/view.html" method="post">
<div class="ui-helper-child-1">
<label class="ui-state-required" for="username">Customer ID:</label>
<input id="username" name="username" maxlength="9" type="text" value="" />
</div>
<div class="ui-helper-child-2">
<label class="ui-state-required" for="password">Password:</label>
<input id="password" name="password" maxlength="20" type="password" value="" />
</div>
<div class="ui-helper-child-3">
<label class="ui-state-required" for="companyId">Company:</label>
<select id="companyId" name="companyId">
<option selected="selected" value="glo">Choose company</option>
<option value="asp">new to asp</option>
<option value="aeg">who is it</option>
<option value="asus">who use this brand name</option>
</select>
</div>
<div class="ui-helper-child-4">
<input id="callback" name="callback" type="hidden" value="/glo/en-us/home,show/view.html?error=1" />
<input type="submit" value="Sign In" />
</div>
</form>
</div>
and one more time how to login? I can add information to by webdriver.FindElement(username) and so on, but not know how to post data as i don't have id or name in submit element.
I think this will work for you:
driver.FindElement(By.CssSelector(".ui-helper-child-4>[type='submit']")).Click();

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?

Resources