【问题标题】:check_box_tag won't pre-checkcheck_box_tag 不会预先检查
【发布时间】:2015-10-30 09:08:14
【问题描述】:

我需要根据用户对问题的回答预先选择一个复选框。我编写了代码来处理响应的保存和使​​用“包含?”的方法。为 check_box_tag 的 "checked" 选项设置 true 或 false 值。但是,无论我做什么,check_box_tag 都拒绝将复选框预先标记为真。我已经审查了与我的问题类似的几乎所有问题和回复,但没有一个答案有效。我将代码简化为以下内容,以删减我能想到的尽可能多的变量:

<% options.each do |option| %>    
  <%= check_box_tag "test", 1, true %>
  <%= option.description %>
<% end %>

复选框与选项说明一起显示,但该复选框仍未预先选中。有没有我遗漏的部分?

检查元素显示:

*input checked="checked" id="test" name="test" type="checkbox" value="1"*

这个特定的代码部分在我通过 locals 传递参数的部分中(如果有帮助的话)。

使用 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 />

--> --> -->
-->

上面的很多代码都已经尝试过了,每个单独的代码都按预期工作......因此我将它分解为原始示例。

_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) 从检查元素添加 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>

Edit(3) 尝试使用自定义属性:

Topic.rb

attr_accessor :response_statement_helper

def response_statement_helper
  true
end

我现在将表单传递到部分中,以允许我以某些简单表单文档显示的方式使用自定义属性...

_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">

但是复选框的显示仍然没有反映“选中”的值。

根据 Gacha 的回答尝试了 edit(3) 的解决方案来参考这个问题: add checkbox with simple_form without association with model?

【问题讨论】:

  • 我认为您可能在问题中消除了太多变量,因为此代码对我有用。如果我将 3 个 &lt;%= check_box_tag "test", 1, true %&gt; 副本放在我的 Rails 应用程序(与您的版本相同)的视图中,我会在浏览器中看到 3 个实际上已选中的复选框。
  • 看起来很奇怪。 checked="checked" 是正确的。也许您应该为复选框粘贴确切的 html 输出。
  • @Sean,这就是问题所在,尽管即使将代码简化为基础,但它仍然对我不起作用......关于其他可能导致此特定问题的任何想法?也许它是一个浏览器问题(虽然我使用的是 chrome,所以我不确定是这种情况)?
  • 我的假设是否正确,因为“checked=checked”应该意味着复选框被标记在视图右侧?
  • &lt;%= check_box_tag "test", 1, checked: true %&gt; 试试这个

标签: ruby-on-rails ruby ruby-on-rails-3 checkbox


【解决方案1】:

看看这个,伙计们!

我不知道为什么这行得通,而我们尝试过的其他事情却没有……但是要让 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>

感谢所有提供意见的人=)

【讨论】:

    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 2016-01-08
    • 1970-01-01
    相关资源
    最近更新 更多