The users will enter students names on forms. How do I intercept the
first and last names and have them stored capitalized when the user
inputs them in lower case?
Do I mess with the form html, skipping form_for helpers and using
conditional clauses in the view?
Do I need to separate out the individual attribute and update it
separately in the controller actions that save/updates the
record?(instead of the blanket object.update_attributes(params[:object])
)
Or, and I think this is what I seem to recall seeing, can I create a
definition in the model that automatically capitalizes given attributes?
I would like code below to work in the Student model(unfortunately its
not working!(and creates a “stack level too deep” error if i leave out
“self”))
I don’t think I really hope to change just what is displayed, or change
what is pulled out over and over again (thats what the accessor would
do?)
I’d prefer to change what is stored ? (thanks for pointing out the
correct way of changing what is retrieved though!)
There must be an idiomatic way of filtering and altering form data going
into records. Is it just to individually set each attribute in the
controller?
There must be an idiomatic way of filtering and altering form data
going
into records. Is it just to individually set each attribute in the
controller?
class MyModel < AR::Base
before_save :capitalize_fields
before_update :capitalize_fields
private
def capitalize_fields
[:first_name, :last_name].each {|field| self[field] = self
[field].capitalize}
end
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.