我有一个带有一些嵌套字段的注册表单,在这个表单中我添加了一个服务条款的复选框。
我尝试验证复选框何时选中,如果没有,则返回错误。
$(".ui.container").on("click", "tr",(function(){
我确实有一个布尔列,在accounts表中设置为默认值。
validates_acceptance_of :agreement, :allow_nil => true, :accept => true, :on => :create
另外,我已将add_column :accounts, :agreement, :boolean, default: false
添加到允许的控制器参数中。
这是表格:
:agreement
但即使我接受它,它也会返回表单错误 <%= 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 %>
。任何想法我可能会遗漏的东西!
答案 0 :(得分:5)
尝试:validates :agreement, acceptance: {accept: true} , on: :create,
allow_nil: false
如果您要验证接受,则不希望:allow_nil => true
。
答案 1 :(得分:2)
您可以更改模型验证并进行测试。
validates :agreement, inclusion: {in: [true]}
或者您可以使用其他验证
validates :agreement, acceptance: { accept: true }