Created
March 3, 2012 00:14
-
-
Save johncblandii/1962769 to your computer and use it in GitHub Desktop.
Acceptance issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 %> |
I'll test the old method later.
Yep. It is a direct port from devise.
def devise_error_messages_gx!
return "" if resource.errors.empty?
```
messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
sentence = I18n.t("errors.messages.not_saved",
:count => resource.errors.count,
:resource => resource.class.model_name.human.downcase)
html = <<-HTML
<div class="alert alert-block alert-error">
<a class="close" data-dismiss="alert">x</a>
<h4 class="alert-heading">#{sentence}</h4>
<ul>#{messages}</ul>
</div>
HTML
html.html_safe
```
end
…---
John C Bland II
http://www.johncblandii.com
http://www.johnandseason.com
On Mar 3, 2012, at 1:09 PM, Jesse Wolgamott wrote:
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?
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1962769
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?
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?