Ruby-on-rails – What is best practices for operating form data before saving in Rails 3?

From a rails best practice point of view, what is the best place to manipulate form data before saving?

For instace, on the contact form, I want to make sure that all data is saved in uppercase (when you submit in the “please contact me” form, don’t you hate people? 🙂 )

>Is it better to operate in the controller? Can I do this in the creation, or move it into some kind of private method, which will capitalize all string properties of the object before saving/update?

Either

>Do you do better in the model before_save?
I think it should be done in the model, because I might want all the records to be the same, no matter if I am in the rake task or manipulate them through the web interface.

Bonus:

If I want to use it on all models and be able to override the default value according to the specific situation, where else would I put it? Application controller?
There may be some special circumstances where you want to save value without capitalization-namely branded products that do not use capital letters (i.e. utorrent) or surnames that should have multiple caps in the name (i.e. Irish and Scottish names such as McDonald’s )

Thank you!

The easiest place is your model. If you think it’s more appropriate, I recommend using before_save or Even before_validation. Something like this can do it:

before_save :upcase_content

def upcase_content
self.content = self.content.upcase
end

In addition, if you want to allow exceptions based on specific circumstances, you can add attr_accessor to the model.

< pre>class MyModel attr_accessor :dont_upcase

before_save :upcase_content, :unless => :dont_upcase

end

Then set the accessor to true when creating the model

@model = Model.new(:brand_name => utorrent)
@model.dont_upcase = true
@model.save!

From the perspective of rails best practices, what is the best place to manipulate form data before saving?

For instace, on the contact form, I want to make sure that all data is saved in uppercase (when you submit in the “please contact me” form, don’t you hate people? 🙂 )

>Is it better to operate in the controller? Can I do this in the creation, or move it into some kind of private method, which will capitalize all string properties of the object before saving/update?

Either

>Do you do better in the model before_save?
I think it should be done in the model, because I might want all the records to be the same, no matter if I am in the rake task or manipulate them through the web interface.

Bonus:

If I want to use it on all models and be able to override the default value according to the specific situation, where else would I put it? Application controller?
There may be some special circumstances where you want to save value without capitalization-namely branded products that do not use capital letters (i.e. utorrent) or surnames that should have multiple caps in the name (i.e. Irish and Scottish names such as McDonald’s )

Thank you!

The easiest place is your model. If you think it is more appropriate, I recommend using before_save or even before_validation. Something like this can do this :

before_save :upcase_content

def upcase_content
self.content = self.content.upcase
end< /pre>

In addition, if you want to allow exceptions based on specific circumstances, you can add attr_accessor to the model.

class MyModel attr_accessor :dont_upcase

before_save :upcase_content, :unless => :dont_upcase
...
end

Then set the accessor when creating the model Is true

@model = Model.new(:brand_name => utorrent)
@model.dont_upcase = true
@model.save!

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3328 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.