Html Rails:form_for 复选框设置为 true 或 false 是否选中/取消选中该框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16844243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: form_for checkbox set to true or false whether the box is checked/unchecked
提问by user2158382
I have a model called users that has 2 boolean attributes send_email
and send_text
. I have a form that edits the User model, and I want it to set those attributes to true/false depending on whether the box is check/unchecked. Here is my form
我有一个名为 users 的模型,它有 2 个布尔属性send_email
和send_text
. 我有一个编辑用户模型的表单,我希望它根据是否选中/取消选中该框将这些属性设置为 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 %>
And here is the update
action of the user_controller
这里是update
的动作user_controller
def update
@user = User.find(params[:id])
@user.update_attributes(params[:user])
redirect_to @user
end
The form and update looks like it works perfectly, but when i submit this form with the send_email or send_text box checked, it does not change the attributes of the user model (send_email,send_text) to false
表单和更新看起来很完美,但是当我在选中 send_email 或 send_text 框的情况下提交此表单时,它不会将用户模型的属性 (send_email,send_text) 更改为 false
回答by Matt
Rails will do this for you when your form is acting on an object, just leave all the extra stuff off the tag like so:
当你的表单作用于一个对象时,Rails 会为你做这件事,把所有额外的东西都放在标签上,像这样:
<div class="field">
<%= f.label "Get Email" %> <br />
<%= f.check_box :send_email %> <br />
</div>
And it should all start working as you expect. The checkboxes will be ticked if the attribute is true, and vice versa the checked state when you submit the form will affect the attribute. Rest of your code is fine.
它应该会按照您的预期开始工作。如果属性为真,复选框将被勾选,反之亦然,提交表单时的选中状态将影响该属性。你的其余代码很好。
回答by tomsihap
Further informations about forms and updating database
有关表单和更新数据库的更多信息
Indeed, the last answer is right : using the form_for syntax is enough and Rails will do the association unchecked:false / checked:true for you.
事实上,最后一个答案是正确的:使用 form_for 语法就足够了,Rails 会为你做关联 unchecked:false/checked:true。
<div class="field"> <%= f.label "Get Email" %> <br /> <%= f.check_box :send_email %> <br /> </div>
<div class="field"> <%= f.label "Get Email" %> <br /> <%= f.check_box :send_email %> <br /> </div>
I had the same problem even with that syntax. The fact is the server's console returned me Unpermitted parameter: checkbox_value
: don't forget to update your required/permitted parameters to put into params
! And in my case :
即使使用该语法,我也遇到了同样的问题。事实是服务器的控制台返回了我Unpermitted parameter: checkbox_value
:不要忘记更新您需要/允许的参数以放入params
!就我而言:
# ***_controller.rb
private
def operator_params
params.require(:operator).permit(:name, :website, :checkbox_value, :global)
end
回答by Dhanshree
I had same problem.I did
我有同样的问题。我做了
<% @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 %>