如何获取空白复选框传递为false到params [英] How to get blank checkboxes to pass as false to params

查看:100
本文介绍了如何获取空白复选框传递为false到params的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单是一个更新用户表单,其中几个元素是复选框。我想真的传递到params如果选中(这是工作)和假如传递到params如果未选中(不工作)。未检查的项目甚至不被发送到params。如何使未检查项目通过为假?



表单

 <%= form_tag user_url(@user) ,class:form-signin,method:'patch'do%> 
< h4>请确认您的电子邮件。如果您有通知,我们只会向您发送电子邮件。< / h4>
<%= email_field_tag:email,current_user.email%>
< h4>想要在有人需要播放器时收到通知?选择以下哪项运动。< / h4>
<%= check_box_tag:basketball,checked = true%>篮球< / br>< / br>
<%= check_box_tag:indoor_volleyball,checked = true%>室内排球< / br>< / br>
<%= check_box_tag:beach_volleyball,checked = true%>沙滩排球< / br>< / br>
<%= check_box_tag:soccer,checked = true%>足球< / br>< / br>
<%= check_box_tag:flag_football,checked = true%>国旗足球< / br>< / br>
<%= check_box_tag:hockey,checked = true%>曲棍球< / br>< / br>
<%= check_box_tag:kickball,checked = true%>踢球< / br>< / br>
<%= check_box_tag:softball,checked = true%>垒球
<%= hidden_​​field_tag:user_id,:value => current_user.id%>
<%= hidden_​​field_tag:user,:value => current_user%>
< div>
< / br>
<%= submit_tag提交,class:btn btn-large btn-success%>
< / div>

控制器

  def update 
respond_to do | format |
if @ user.update(update_params)
format.html {redirect_to @user,notice:'Updates being 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


解决方案

复选框具有空值,例如:

 <%= hidden_​​field_tag:basketball,''%> 
<%= check_box_tag:basketball,checked = true%>篮球< / br>< / br>

然后窗体意识到如果没有选择,需要用空值填充该字段。 / p>

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?

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

解决方案

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.

这篇关于如何获取空白复选框传递为false到params的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆