Rails:form_for复选框设置为true或false是否选中/取消选中该框

时间:2013-05-30 19:21:15

标签: html ruby-on-rails ruby forms

我有一个名为users的模型,它有2个布尔属性send_emailsend_text。我有一个编辑User模型的表单,我希望它将这些属性设置为true / false,具体取决于是否选中此复选框。这是我的表格

<%= form_for(@user) do |f| %>
    <div class="field">
        <%= f.label :email %> <br />
        <%= f.text_area :email %> <br />
    </div>
    <div class="field">
        <%= f.label :cell %> <br />
        <%= f.text_area :cell %> <br />
    </div>
    <div class="field">
        <%= f.label "Get Email" %> <br />
        <%= f.check_box :send_email, {}, true, false %> <br />
    </div>
    <div class="field">
        <%= f.label "Get Text" %> <br />
        <%= f.check_box :send_text, {}, true, false %> <br />
    </div>
    <div class="actions">
  <%= f.submit "Submit", class: "button small radius" %>
  <%= link_to "go back", @user, class: "button small radius secondary" %>
</div>
<% end %>

以下是update

user_controller操作
def update
    @user = User.find(params[:id])
    @user.update_attributes(params[:user])
    redirect_to @user
end

表单和更新看起来效果很好,但是当我在选中send_email或send_text框提交此表单时,它不会将用户模型的属性(send_email,send_text)更改为false

4 个答案:

答案 0 :(得分:48)

当你的表单作用于某个对象时,Rails会为你做这件事,只需将所有额外的东西留在标签上,如下所示:

<div class="field">
    <%= f.label "Get Email" %> <br />
    <%= f.check_box :send_email %> <br />
</div>

它应该按照你的预期开始工作。如果属性为true,则会勾选复选框,反之亦然,当您提交表单时,选中状态将影响该属性。你的其余代码很好。

答案 1 :(得分:3)

有关表单和更新数据库的更多信息

事实上,最后一个答案是正确的:使用form_for语法就足够了,Rails将取消选中关联:false / checked:true for you。

<div class="field">
    <%= f.label "Get Email" %> <br />
    <%= f.check_box :send_email %> <br />
</div>

即使使用该语法,我也遇到了同样的问题。事实是服务器的控制台让我Unpermitted parameter: checkbox_value:不要忘记更新您所需/允许的参数以放入params!就我而言:

# ***_controller.rb
      private
        def operator_params
          params.require(:operator).permit(:name, :website, :checkbox_value, :global)
        end

答案 2 :(得分:0)

我有同样的问题。我做了

<% @batches.each do |batch| %>
  <div class="name_list<%=cycle('odd', 'even')%>"><li>
    <label><%= check_box_tag "send_sms[batch_ids][]",  batch.id,false,:class=>'right' %>
   <div class="att_list_names"> <%= batch.full_name %></div> </label>
   </li>  </div>
  <% end %>

答案 3 :(得分:0)

我有类似的问题。我首先通过向用户添加教师角色来解决该问题,并设置为布尔值并将其设置为<div class="container"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> <div class="item">6</div> <div class="item">7</div> <div class="item">8</div> <div class="item">9</div> <div class="item">10</div> <div class="item">11</div> <div class="item">12</div> <div class="item">13</div> <div class="item">14</div> <div class="item">15</div> <div class="item">16</div> <div class="item">17</div> <div class="item">18</div> <div class="item">19</div> <div class="item">20</div> <div class="item">21</div> <div class="item">22</div> <div class="item">23</div> <div class="item">24</div> <div class="item">25</div> <div class="item">26</div> <div class="item">27</div> <div class="item">28</div> <div class="item">29</div> <div class="item">30</div> <div class="item">31</div> <div class="item">32</div> <div class="item">33</div> <div class="item">34</div> <div class="item">35</div> <div class="item">36</div> <div class="item">37</div> <div class="item">38</div> <div class="item">39</div> <div class="item">40</div> <div class="item">41</div> <div class="item">42</div> <div class="item">43</div> <div class="item">44</div> <div class="item">45</div> <div class="item">46</div> <div class="item">47</div> <div class="item">48</div> <div class="item">49</div> <div class="item">50</div> </div>

default:false

然后创建一个注册为:

rails generate migration add_teacher_to_users teacher:boolean