我需要根据用户对问题的回答预先选择一个复选框。我编写了代码来处理保存响应和方法使用"包括?"为"设置"设置真值或假值check_box_tag的选项。但是,无论我做什么,check_box_tag都拒绝将复选框标记为true。我几乎回答了与我的问题类似的所有问题和回答,但没有一个答案奏效。我将我的代码简化为以下内容,以删除我能想到的许多变量:
<% options.each do |option| %>
<%= check_box_tag "test", 1, true %>
<%= option.description %>
<% end %>
复选框显示选项说明,但复选框仍未预先检查。我有一件遗失的东西吗?
检查元素显示:
*input checked="checked" id="test" name="test" type="checkbox" value="1"*
代码的这一特定部分属于我通过本地传递参数的部分内容(如果有帮助的话)。
使用Rails 3.2和Ruby 1.9.3
这是与collection_check_boxes类似的问题 How to pre-populate collection_check_boxes in an edit form?
感谢您抽出时间与我联系=)
编辑1(为上下文添加代码): 为了进一步说明我们正在处理嵌套表单......
_multiple_choice_mr_form.html.erb
<%= question_statement %> <br/>
<% response_choices.each do |choice| %>
<!--% raise choice.inspect %>-->
<!--% raise "#{Response.response_answered?(current_user.id, question_id)} ~" + "question id: #{question_id}" %>-->
<!--% raise Response.multiple_answers_checked?(current_user.id, question_id, choice.description).inspect %>-->
<!--%= check_box_tag "response_#{question_id}_#{choice.id}".to_sym, choice.description, Response.multiple_answers_checked?(current_user.id, question_id, choice.description) %>-->
<%= check_box_tag "test", 1, true %>
<%= choice.description %> <br />
- &GT;
- &GT;
- &GT;
- &GT;
上面的代码已经尝试了很多代码,并且代码的每一段都按预期工作......因此,为什么我只是将其分解为原始示例。
_form.html.erb
<table>
<% @topic.topic_pages.order("sort_field asc").each do |topic_page| %>
<tr>
<h2><%= topic_page.title %></h2>
<% topic_page.page_sections.order("sort_field asc").each do |page_section| %>
<p>
<h3><%= page_section.title if page_section.title.present? %></h3>
<%= page_section.content.html_safe if page_section.content.present? %>
<%= page_section.instruction.html_safe if page_section.instruction.present? %>
<% if page_section.upload_material.present? %>
<%= image_tag page_section.upload_material.url %>
<% end %>
<% page_section.questions.order("question_order asc").each do |question| %>
<% if question.question_type == "No Response" %>
<%= question.question_statement %> <br />
<% else %>
<%= render :partial => 'responses/online_document_responses/mulitple_choice_form', :locals => {:question_id => question.id, :question_statement => question.question_statement} if question.completed? %>
<%= render :partial => 'responses/online_document_responses/radio_button_form', :locals => {:question_id => question.id, :question_statement => question.question_statement, :response_choices => question.answer_choice_options} if question.scale? %>
<%= render :partial => 'responses/online_document_responses/open_response_form', :locals => {:question_id => question.id, :question_statement => question.question_statement} if question.open_response? %>
<%= render :partial => 'responses/online_document_responses/ranking_form', :locals => {:question_id => question.id, :question_statement => question.question_statement, :response_choices => question.answer_choice_options} if question.ranking? %>
<%= render :partial => 'responses/online_document_responses/multiple_choice_sr_form', :locals => {:question_id => question.id, :question_statement => question.question_statement, :response_choices => question.answer_choice_options} if question.multiple_choice_sr? %>
<%= render :partial => 'responses/online_document_responses/multiple_choice_mr_form', :locals => {:question_id => question.id, :question_statement => question.question_statement, :response_choices => question.answer_choices} if question.multiple_choice_mr? %>
<% end %>
<% end %>
<% end %>
</tr>
<% end %>
</table>
<div class="form-actions">
<%= f.button :submit, value: "Save Responses" %>
</div>
**编辑(2)从inspect元素添加HTML输出:
<body id="gl">
<div id="main" role="main">
<div class="block" style="padding: 4em 1.25em;">
<div class="g center">
<div class="u100">
<form accept-charset="UTF-8" action="/topics/2" class="simple_form edit_topic" id="edit_topic_2" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="put"><input name="authenticity_token" type="hidden" value=“omitted"></div>
Mark all that apply! <br><input checked="checked" id="test" name="test" type="checkbox" value="1">
one <br><input checked="checked" id="test" name="test" type="checkbox" value="1">
two <br><input checked="checked" id="test" name="test" type="checkbox" value="1">
three <br><input checked="checked" id="test" name="test" type="checkbox" value="1">
four <br><input checked="checked" id="test" name="test" type="checkbox" value="1">
five <br><input checked="checked" id="test" name="test" type="checkbox" value="1">
six <br>
<div class="form-actions">
<input class="btn" name="commit" type="submit" value="Save Responses">
</div>
</form> <br>
</div>
</div>
</div>
</div>
</body>
编辑(3)尝试使用自定义属性:
Topic.rb
attr_accessor :response_statement_helper
def response_statement_helper
true
end
我现在将表单传递给partial以允许我以简单表单的一些文档显示的方式使用自定义属性...
_multiple_choice_mr_form.html.erb
-<%= check_box_tag "test", 1, true %>
+<%= form.input_field :response_statement_helper, as: :boolean, boolean_style: :inline %>
这在html输出中产生:
<input boolean_style="inline" checked="checked" class="boolean optional" id="topic_response_statement_helper" name="topic[response_statement_helper]" type="checkbox" value="1">
但是复选框的显示仍然不能反映&#34;已检查&#34;值。
在Gacha的回复中尝试编辑(3)解决这个问题的方法: add checkbox with simple_form without association with model?
答案 0 :(得分:0)
检查出来的人!
我不知道为什么会这样,而我们尝试过的其他事情却没有......但是对于simple_form_for来说,要让你的复选框预先检查这一行和语法:
#for your code....
<%= form.input :your_attribute, :as => :boolean, input_html: {checked: true} %>
#for my specific code I used
<%= form.input "response_#{question_id}_#{choice.id}".to_sym, :as => :boolean, input_html: {checked: true} %>
#My html output:
<div class="input boolean optional topic_response_220_599"><input name="topic[response_220_599]" type="hidden" value="0"><input checked="checked" class="boolean optional" id="topic_response_220_599" name="topic[response_220_599]" type="checkbox" value="1"><label class="boolean optional control-label" for="topic_response_220_599">Response 220 599</label></div>
感谢所有提出意见的人=)