Skip to content

Instantly share code, notes, and snippets.

@johncblandii
Created March 3, 2012 00:14
Show Gist options
  • Save johncblandii/1962769 to your computer and use it in GitHub Desktop.
Save johncblandii/1962769 to your computer and use it in GitHub Desktop.
Acceptance issues
//Model - acknowledges validation but never approves it
validates :agreed_to_terms, :acceptance => true, :allow_nil => false
//Model - completely ignores validation
validates :agreed_to_terms, :acceptance => true
//Full Model
class User < ActiveRecord::Base
self.table_name = 'users'
# Include default devise modules. Others available are:
# :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :token_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
extend ::FriendlyId
friendly_id :username
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :is_active, :last_known_region, :username, :first_name, :middle_initial, :last_name, :suffix, :gender, :about, :agreed_to_terms, :dob
validates :agreed_to_terms, :acceptance => true, :allow_nil => true
validates :username, :length => {:in => 3..20}, :presence=>true, :uniqueness=>true
validate :validate_user_is_old_enough, :on=>:create
private
def validate_user_is_old_enough
if dob.nil?
errors.add(:dob, "must be provided")
else
errors.add(:dob, "must be more than 13 years ago") if dob > 13.years.ago
end
end
end
//Form
<%= form_for @user, :url=>signup_path do |f| %>
<%= devise_error_messages_gx! %>
<div>
<%= f.check_box(:agreed_to_terms, :class=>"inline") %>
<%= f.label(:agreed_to_terms, "I Agree to the Terms and Conditions", :class=>"inline") %>
</div>
<div>
<%= submit_tag "Sign Up!", :class=>"btn-primary", :value=>"Sign Up!" %>
<% end %>
@jwo
Copy link

jwo commented Mar 3, 2012

Does devise_error_messages_gx write out all other error messages in addition to devise errors?

Also, if you change to the older format below, do you still get problem?

validates_acceptance_of :agreed_to_terms, :allow_nil => false

And of course, by "problem" -- what do we mean? Is it that the form shows with no error message but clearly does not save? Or does it tell you that "agreed_to_terms" must be accepted?

@johncblandii
Copy link
Author

johncblandii commented Mar 3, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment