简体   繁体   English

rails 中 form_helper 的 Check_box 和 radio_button

[英]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.我浏览了 rails 的 rails 文档,找不到任何地方可以提供有关check_boxradio_button的信息来处理 model 对象。

    = 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 .我用radio_button试过这个,但值只是没有从params中的表单传递。 Same case with the check_box .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.有人可以向我解释为什么会发生这种情况,以及为什么 rails 没有指定使用 Model 对象的check_boxradio_button

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'.这是官方文档中给出的示例,两个复选框的值都与“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. https://api.rubyonrails.org/v5.1.7/classes/ActionView/Helpers/FormOptionsHelper.html看看这个。

    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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM