簡體   English   中英

rails 中 form_helper 的 Check_box 和 radio_button

[英]Check_box and radio_button for form_helper in rails

我瀏覽了 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?"

我用radio_button試過這個,但值只是沒有從params中的表單傳遞。 check_box相同的情況。

有人可以向我解釋為什么會發生這種情況,以及為什么 rails 沒有指定使用 Model 對象的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”相同。 很難理解這里發生了什么。

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