I need a checkbox to be preselected based off of a users response to a question. I wrote the code to handle the saving of responses and the method using "includes?" to set a true or false value for the "checked" option of the check_box_tag. However, the check_box_tag refuses to premark the checkbox as true no matter what I do. I've reviewed almost every question and response similar to my question and none of the answers have worked. I simplified my code to the following to cut out as many of the variables I could think of:
<% options.each do |option| %>
<%= check_box_tag "test", 1, true %>
<%= option.description %>
<% end %>
The check boxes are displayed with the option descriptions, but the check box still isn't pre-checked. Is there a piece to this I'm missing?
inspect elements shows this:
*input checked="checked" id="test" name="test" type="checkbox" value="1"*
This particular portion of code is within a partial where I'm passing in the params via locals(if that helps).
Using Rails 3.2 and Ruby 1.9.3
This is a similar issue to but with collection_check_boxes How to pre-populate collection_check_boxes in an edit form?
I appreciate your time in checking this out with me =)
Edit 1(addition of code for context): To give further context we are dealing with nested forms...
_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 />
-->
-->
-->
-->
Alot of the code above has been tried and each individual piece of the code is working as intended... Hence why I've just broken it down to the original example.
_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>
**Edit(2) Adding HTML output from inspect element:
<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>
Edit(3) Tried using custom attributes:
Topic.rb
attr_accessor :response_statement_helper
def response_statement_helper
true
end
And I am passing the form now into the partial to allow me to use the custom attribute in the way some of the documentation for simple form shows...
_multiple_choice_mr_form.html.erb
-<%= check_box_tag "test", 1, true %>
+<%= form.input_field :response_statement_helper, as: :boolean, boolean_style: :inline %>
This produces in the html output:
<input boolean_style="inline" checked="checked" class="boolean optional" id="topic_response_statement_helper" name="topic[response_statement_helper]" type="checkbox" value="1">
But the display of the checkboxes still does not reflect the "checked" value.
Attempted edit(3)'s solution in reference to this question with Gacha's response: add checkbox with simple_form without association with model?