0

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?

Community
  • 1
  • 1
  • I think you might have eliminated too many variables in your question, because this code works for me. If I put 3 copies of `<%= check_box_tag "test", 1, true %>` in a view my Rails app (same versions as yours), I see 3 checkboxes in my browser that are in fact checked. – Sean Huber Aug 07 '15 at 05:14
  • Seems weird. `checked="checked"` is correct. Maybe you should paste the exact html output for the checkboxes. –  Aug 07 '15 at 05:20
  • @Sean, That is the issue though even when the code is simplified to the basics it still isn't working for me...Any thoughts on what other things could cause this particular issue? Perhaps its a browser issue (I am using chrome though so I'm not sure that is the case)? – HotShot3150 Aug 07 '15 at 07:14
  • Am I correct in assuming because the "checked= checked" that should mean the checkbox is marked on the view right? – HotShot3150 Aug 07 '15 at 08:01
  • `<%= check_box_tag "test", 1, checked: true %>` try this – Vrushali Pawar Aug 07 '15 at 13:43
  • If you comment out your entire view and just have single line: `<%= check_box_tag "test", 1, true %>`, does a checked checkbox get rendered? – Sean Huber Aug 07 '15 at 14:22
  • @RORDeveloper I tried the code using checked: true and still nothing... – HotShot3150 Aug 07 '15 at 20:11
  • @Sean Huber The output is the same even when I remove everything. upon chatting with some colleagues I think it may be an issue with simple_form_for that is causing the issue. – HotShot3150 Aug 07 '15 at 20:11
  • I created a new project just to test the check_box_tag logic and it worked properly as you said @Sean. Question is what is keeping the logic from displaying the checkbox properly? Thoughts? Thanks guys for your continued input =) – HotShot3150 Aug 07 '15 at 22:38

1 Answers1

0

Check this out guys!

I don't know why this works and the others things we tried didn't... But for simple_form_for to get your checkboxes to pre-check do this line and syntax:

 #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>

Thanks to all who gave their input =)