Rails: Form_for fields without corresponding database columns
I'm trying to do up a rails form_for that includes a checkbox for :terms_and_conditions
(with validates_presence_of
). terms_and_conditions
has not been declared as a column in the corresponding database table.
At the moment, when I submit the form and try to create a new model with MyModel.new(params[:mymodel])
, I get an *'unknown attribut开发者_如何转开发e: terms_and_conditions'* error.
So, just wondering: how do I run create a field for a column - and run validations on it - without inserting that field into the database?
Many thanks...
You need a virtual attribute for terms_and_conditions in your model. Take a look at this screencast
For accepting T&Cs only, you should now be able to just put this in your model:
validates_acceptance_of :terms_and_conditions
精彩评论