我想在表单中添加简单的术语验证器:
模型
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
我做错了什么?
答案 0 :(得分:1)
您可以尝试:
field :terms, :type => Boolean, :default => false
attr_accessible :terms
validates :terms, :acceptance => {:accept => true}
在你看来:
<%= f.input :agree, :as => :boolean, label: false %>
问候!
答案 1 :(得分:0)
您不需要列或attr_accessor
,否则它将是一个类对象。
你能做的是
attr_accessible :terms
validates_acceptance_of :terms
<%= f.check_box :terms%> I agree to ....