Rails中的form_helper的Check_box和radio_button

时间:2019-10-17 11:33:53

标签: ruby-on-rails ruby form-helpers

我遍历了rails的rails文档,找不到任何可以给我有关return falsecatch信息的信息来处理模型对象。

check_box

我使用radio_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相同。

有人可以向我解释为什么会发生这种情况,以及为什么rails并未指定模型对象使用paramscheck_box的原因。

也是

check_box

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

1 个答案:

答案 0 :(得分: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>
相关问题