Ruby-on-rails 如何让空白复选框作为 false 传递给 params

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20379311/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:06:18  来源:igfitidea点击:

How to get blank checkboxes to pass as false to params

ruby-on-railscheckboxruby-on-rails-4

提问by brad

I have a form that is an update user form where several of the elements are checkboxes. I want true to pass to the params if checked (this is working) and false to pass to the params if unchecked (not working). The unchecked items are not even being sent to the params. How can i make an unchecked item pass through as false?

我有一个表单,它是一个更新用户表单,其中几个元素是复选框。如果选中(这是有效的),我希望 true 传递给参数,如果未选中(不起作用),则传递给参数 false 。未经检查的项目甚至没有被发送到参数。我怎样才能让一个未经检查的项目作为假通过?

Form

形式

<%= form_tag user_url(@user), class: "form-signin", method: 'patch' do %>
 <h4>Please confirm your email.  We'll only email you if you have notifications.</h4>
  <%= email_field_tag :email, current_user.email %>
 <h4>Want to be notified when someone needs a player?  Choose which sports below.</h4>
  <%= check_box_tag :basketball, checked = true %> Basketball</br></br>
  <%= check_box_tag :indoor_volleyball, checked = true %> Indoor Volleyball</br></br>
  <%= check_box_tag :beach_volleyball, checked = true %> Beach Volleyball</br></br>
  <%= check_box_tag :soccer, checked = true %> Soccer</br></br>
  <%= check_box_tag :flag_football, checked = true %> Flag Football</br></br>
  <%= check_box_tag :hockey, checked = true %> Hockey</br></br>
  <%= check_box_tag :kickball, checked = true %> Kickball</br></br>
  <%= check_box_tag :softball, checked = true %> Softball
  <%= hidden_field_tag :user_id, :value => current_user.id %>
  <%= hidden_field_tag :user, :value => current_user %>
 <div>
  </br>
  <%= submit_tag "Submit", class:"btn btn-large btn-success" %>
 </div>

Controller

控制器

  def update
   respond_to do |format|
   if @user.update(update_params)
     format.html { redirect_to @user, notice: 'Updates were successful.' }
     format.json { head :no_content }
    else
     format.html { render action: 'edit' }
     format.json { render json: @user.errors, status: :unprocessable_entity }
    end
   end
  end

  def update_params
    params.permit(:email, :soccer, :softball, :beach_volleyball, :indoor_volleyball, :flag_football, :basketball, :hockey, :kickball)
  end

回答by JellyFishBoy

You need to place a hidden field tag before each checkbox with an empty value, for example:

您需要在每个带有空值的复选框之前放置一个隐藏字段标记,例如:

<%= hidden_field_tag :basketball, '' %>
<%= check_box_tag :basketball, checked = true %> Basketball</br></br>

Then the form is aware it needs to populate that field with an empty value if nothing is selected.

然后表单知道如果没有选择任何内容,它需要用空值填充该字段。

回答by viks

If anyone have column type boolean and using check_box_tag then look at this. It worked for me. <%= hidden_field_tag :basketball, 'false' %> <%= check_box_tag :basketball, true, is_checked? %>

如果有人有列类型 boolean 并使用 check_box_tag 然后看看这个。它对我有用。 <%= hidden_field_tag :basketball, 'false' %> <%= check_box_tag :basketball, true, is_checked? %>

回答by gotva

Look at it

看它

The main idea to place hidden field before checkbox. When a form is submitted fields will be parsed: if checkbox is checked - its value will be passed, else the value of hidden field

在复选框之前放置隐藏字段的主要思想。当表单提交时,字段将被解析:如果复选框被选中 - 它的值将被传递,否则隐藏字段的值

For example smth like this (NOT testes)

例如像这样(不是睾丸)

<%= hidden_field_tag :basketball, false %>
<%= check_box_tag :basketball, checked = true %> Basketball</br></br>

回答by CaliCanadian

The problem is if you use a hidden attribute before a checkbox and give it the same reference like this:

问题是如果您在复选框之前使用隐藏属性并为其提供相同的引用,如下所示:

<%= hidden_field_tag :basketball, false %>
<%= check_box_tag :basketball, checked = true %> Basketball</br></br>

Then if you want to do any type of dynamic action on an attribute, say like $("#basketball").removeAttr("checked")it will target the first instance of $("#basketball")which in this case is the hidden object.

然后,如果您想对某个属性执行任何类型的动态操作,例如$("#basketball").removeAttr("checked")它会以第一个实例为目标$("#basketball"),在这种情况下是隐藏对象。

What I ended up doing in my implementation was to add a default value of false to my checkboxes since my default setting was to be unchecked. Then in my jquery I detected a change on the checkbox elements and grabbed the ID of that checkbox. When a change is detected I evaluated the checked property for true or false and set the value attribute accordingly.

我最终在我的实现中做的是在我的复选框中添加一个默认值 false ,因为我的默认设置是未选中的。然后在我的 jquery 中,我检测到复选框元素的更改并获取了该复选框的 ID。当检测到更改时,我评估了检查属性的 true 或 false 并相应地设置 value 属性。

$(".checkbox").on('change', function(e) {
  var that;
  that = "#" + e.target.getAttribute("id");
  if ($(that).prop("checked") === true) {
    $(that).val("true");
  }
  if ($(that).prop("checked") === false) {
    return $(that).val("false");
  }
});

When I submit my PUT method form action it is updating the database record correctly with the boolean value even if it is blank.

当我提交我的 PUT 方法表单操作时,即使它为空,它也会使用布尔值正确更新数据库记录。

回答by Kyle

It appears that you are able to pass the value. See the following code:

看来您可以传递该值。请参阅以下代码:

check_box_tag 'accept'
<input id="accept" name="accept" type="checkbox" value="1" />

check_box_tag 'rock', 'rock music'
<input id="rock" name="rock" type="checkbox" value="rock music" />

check_box_tag 'receive_email', 'yes', true
<input checked="checked" id="receive_email" name="receive_email" type="checkbox" value="yes" />

check_box_tag 'tos', 'yes', false, :class => 'accept_tos'
<input class="accept_tos" id="tos" name="tos" type="checkbox" value="yes" />

check_box_tag 'eula', 'accepted', false, :disabled => true
<input disabled="disabled" id="eula" name="eula" type="checkbox" value="accepted" />

http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag

http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag