Optimal method to process form params before DB storage?
I'd like to process the info in my form params before they enter the database, I just want to know what the optimal method is to do this.
For example, for my users model, should I add a method to each expected param, for example:
开发者_如何学Cdef first_name=(name)
self.first_name = name.capitalize.strip
end
Or should I modify the form params in another way?
Your approach will probably result in a stack overflow. You are calling the first_name= function recursively as every time you set self.first_name it is calling first_name=
The correct way to do this is as follows :-
def first_name=(name)
write_attribute( :name, name.capitalize.strip)
end
精彩评论