Ruby on Rails: active record override password validations
by bseanvt
leave a comment
Change database column names for form validations in Rails
When you use validations in Rails, db column names are used as ‘keys’ for error messages. This is usually the preferred way to go about it because this maps nicely to the form fields. However, if you use a virtual attribute this may not be the case. For example, I have a ‘password_crypted’ field in my users table that I don’t want my user to see if they fail to complete the field. Instead of returning “Password crypted cannot be blank” I just want to tell them that a password can’t be blank. If you provide a custom ‘:message’ on the validation this won’t replace the column name. The solution is to override the “human_attribute_name” class method and map specific column names to the string you want to use instead.
class User < ActiveRecord::Base
validates_presence_of :password_crypted
ATTR_NAMES = {:password_crypted => "Password"}
def self.human_attribute_name(attr)
ATTR_NAMES[attr.to_sym] || super
end
end
I found these resources helpful while I was in search for a solution to this problem.
http://stackoverflow.com/questions/808547/fully-custom-validation-error-message-with-rails
http://henrik.nyh.se/2007/12/change-displayed-column-name-in-rails-validation-messages
Ruby on Rails: attr_accessor bug data type datetime validations
by bseanvt
leave a comment
Problem slash Bug in Rails with attr_accessor and Datetime Select Fields
Looks like there is a problem with using the attr_accessor method with datetime form fields http://dev.rubyonrails.org/ticket/8983 In Rails 2.3.2, for me at least, the problem seems to be popping up again. While trying to process a date for credit card validation, I keep getting a ‘nil.klass’ error. Sure enough, remove the datetime select from the form and the problem goes away. I need to look into this a little further because I think that this has been resolved.
More information is available here
http://www.ruby-forum.com/topic/130229
and
http://www.google.com/search?q=1+error(s)+on+assignment+of+multiparameter+attributes&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
The temporary solution is to not use attr_accessor and rather add the field to the database with the correct data type
t.datetime :your_datetime_attribute_you_were_once_virtualizing
. Everything then works as normal. Oh well…
Ruby on Rails: attr_accessor forms getters Rails setters troubleshooting validations virtual attributes
by bseanvt
leave a comment
Trouble Using Attr_Accessor in Rails Models and Forms
You might use the attr_accessible method to create getters and setters for a class that has attributes which don’t map directly to corresponding fields in a database. For example let’s take the scenario where you are processing a credit card transaction. You don’t want to save the credit card details, such as card number and verification value etc, however you still want to use these attributes in a form and you want to perform validations on them.
class CreditCardPurchase < ActiveRecord::Base attr_accessor :number, :cvv end
The only probem that I’ve run into is using the attr_accessor on datetime select form fields. Rails won’t be able to determine the “klass” and will spit out a nasty error. Looks there is some discussion around this bug. I’ve got another post about this topic http://seanbehan.com/ruby-on-rails/problem-slash-bug-in-rails-with-attr_accessor-and-datetime-select-fields/ but unfortunately not a whole lot of resultion :(


