导轨中的form_helper的复选框和单选按钮

问题描述 投票:0回答:1

我浏览了rails的rails文档,找不到任何可以给我有关check_boxradio_button信息的信息来处理模型对象。

    = f.radio_button(:recurring_status, true)
    = f.label :recurring_status, "Yes?"
    = f.radio_button(:recurring_status, false)
    = f.label :recurring_status_false, "No?"

我尝试使用radio_button进行此操作,但该值只是未从params中的表格传递。与check_box相同。

[有人可以向我解释为什么会发生这种情况,以及为什么Rails并未指定模型对象使用check_boxradio_button

也,

<%= 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>

这是官方文档中给出的示例,两个复选框的值均与'1'相同。很难理解这里发生了什么。

ruby-on-rails ruby form-helpers
1个回答
0
投票

[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>
© www.soinside.com 2019 - 2024. All rights reserved.