【问题标题】:How to properly add acceptance validator to form in Rails 3.2?如何在 Rails 3.2 中正确添加验收验证器?
【发布时间】:2013-01-21 22:15:28
【问题描述】:

我想在我的表单中添加简单的条款验证器:

型号

attr_accessor :terms
validates :terms, :acceptance => {:accept => true}, :allow_nil => false

查看(simple_form)

= simple_form_for @student, html: { multipart: true } do |f|
   ...
= f.input :terms, as: :select
= f.button :submit

但我明白了:

Can't mass-assign protected attributes: terms

我做错了什么?

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-3 forms validation


【解决方案1】:

你可以试试:

field :terms, :type => Boolean, :default => false
attr_accessible :terms
validates :terms, :acceptance => {:accept => true}

在你看来:

<%= f.input :agree, :as => :boolean, label: false %> 

问候!

【讨论】:

  • 嗨,我在发送检查条款时出错 => ... "terms"=&gt;"0"}
  • 您必须在模型中添加一个布尔字段。我已经编辑了我的回复。例如使用 mongoid 你可以添加到你的模型field :terms, :type =&gt; Boolean, :default =&gt; false,如果你使用 activerecord 你已经进行了迁移!
【解决方案2】:

您不需要列或attr_accessor,否则它将是一个类对象。

你能做的是

attr_accessible :terms
validates_acceptance_of :terms

<%= f.check_box :terms%> I agree to ....

【讨论】:

    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多