gpt4 book ai didi

ruby-on-rails - rails 5 : validation of acceptance for a checkbox

转载 作者:行者123 更新时间:2023-12-04 05:53:21 25 4
gpt4 key购买 nike

我有一个带有一些嵌套字段的注册表单,在此表单中我添加了一个服务条款复选框。

我正在尝试验证复选框是否被选中,如果没有则返回错误。

 validates_acceptance_of :agreement, :allow_nil => true, :accept => true, :on => :create

我确实有一个 bool 列,该列在帐户表中设置为默认值。
add_column :accounts, :agreement, :boolean, default: false

另外,我添加了 :agreement到允许的 Controller 参数。

这是表格:
   <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<div class="centerList">
<div id="error_explanation">
<%= devise_error_messages! %></div>
</div>

<div class="form-group">
<span class="asterisk_input"></span>
<%= f.email_field :email, autofocus: true, placeholder: "email", :class=>"form-control" %>
</div>

<div class="form-group">
<span class="asterisk_input"></span>
<%= f.password_field :password, placeholder: "password", autocomplete: "off", :class=>"form-control" %>
</div>

<div class="form-group">
<span class="asterisk_input"></span>
<%= f.password_field :password_confirmation, placeholder: "confirm password", autocomplete: "off", :class=>"form-control" %>
</div>

<%= f.fields_for :account do |form| %>


<div class="form-group">
<span class="asterisk_input"></span>
<%= form.text_field :street, placeholder: "street", :class=>"form-control" %>
</div>

<div class="form-group">
<span class="asterisk_input"></span>
<%= form.text_field :city, placeholder: "city", :class=>"form-control" %>
</div>

<div class="form-group">
<span class="asterisk_input"></span>
<%= form.text_field :state, placeholder: "state", :class=>"form-control" %>
</div>

<div class="form-group">
<span class="asterisk_input"></span>
<%= form.text_field :zip_code, placeholder: "zip code", :class=>"form-control" %>
</div>

<div class="form-group">
<span class="asterisk_input"></span>
<%= form.country_select :country, ["US"], {}, { :class => "form-control", :id=>"sign-frm-input" } %>
</div>

<div class="form-group">
<%= form.check_box :agreement %> I agree to the <%= link_to 'Terms', term_path(:id=>1) %>.
</div>

<% end %>
<div class="form-group">
<%= f.submit "Sign up", :class=>'btn btn-primary' %>
<% end %>

但它返回表单错误 agreement must be accepted即使我确实接受了。任何想法我可能会错过ohere!

最佳答案

试试:validates :agreement, acceptance: {accept: true} , on: :create,
allow_nil: false

你不想:allow_nil => true如果您正在验证接受。

关于ruby-on-rails - rails 5 : validation of acceptance for a checkbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48522946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com