我遍历了rails的rails文档,找不到任何可以给我有关return false
或catch
信息的信息来处理模型对象。
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并未指定模型对象使用params
和check_box
的原因。
也是
check_box
这是官方文档中给出的示例,两个复选框的值均与“ 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>