在 Rails 3 中保存之前操作表单数据的最佳实践位置是什么?
从 Rails 最佳实践的角度来看,保存之前操作表单数据的最佳位置是什么?
例如,在联系表单上,我想确保所有数据都以大写形式保存(您不讨厌人们在提交“请联系我”表单时对您大喊大叫吗?:-)
- )在控制器中进行操作?我可以在创建中执行此操作,或者将其移至某种私有方法中,该方法将在保存/更新之前将对象的所有字符串属性大写?
还是
- 最好在模型 before_save 中执行? 对我来说,这应该在模型中完成,因为我可能希望所有记录都相同,无论我是在 rake 任务中还是通过 Web 界面操作它们。
奖励:
如果我希望在我的所有模型上都使用它,并且能够根据具体情况覆盖默认值,我应该将其放置在哪里?应用控制器? 可能有一些特殊情况,您希望在不大写的情况下节省价值 - 即不大写的品牌产品(即 utorrent)或姓氏应在名称中包含多个大写字母(即爱尔兰和苏格兰名称,如 McDonald)
谢谢你!
From the pointview of rails best practices, what is the best place to manipulate form data before saving?
For instace, on a contact form, I want to make sure that all data is saved in capitalized form ( don't you hate when PEOPLE SHOUT AT YOU in their "please contact me" form submission? :-) )
- is it better to do manipulation in controller? I could either do it in create, or move it into some sort of private method , that will capitalize all string attributes of the object before saving / updating?
Or
- is it better do in the model before_save?
It makes sense to me that it should be done in the model since I probably want that to be the same for all records, no matter whether I manipulate on them in a rake task or through the web interface.
Bonus:
Also where would I place it if I want that that on ALL my models, with the ability to override default on a case by case basis? Application controller?
There might be some special cases where you want to save value without capitalizing - i.e. brand name products that don't capitalize (i.e. utorrent) or a last name that should have multiple caps in the name (i.e. Irish & Scottish names like McDonald)
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的地方就是在你的模型中。如果您觉得更适合,我建议使用 before_save 甚至 before_validation 。像这样的事情就可以解决问题:
此外,如果您想允许具体情况的例外,您可以向模型添加 attr_accessor 。
然后当您创建模型时将访问器设置为 true
the easiest place to put this is in your model. I would suggest using either before_save or even before_validation if you feel that fits better. Something like this would do the trick:
Additionally if you wanted to allow for exceptions of a case by case basis you could add an attr_accessor to your model.
then when you create a model set the accessor to true
放置它的最佳位置是在您的模型中,这样您就有一个胖模型和一个瘦控制器,这是一件“好事”。
如果您希望所有模型都可以使用此功能,我的建议是使用包含共享功能的模块,然后将其包含在您想要具有默认行为的所有模型中。
The best place to put this is in your model, that way you have a fat model and a skinny controller, which is a "good thing".
If you want to have this be available for all of your models my suggestion is to use a module which contains your shared functionality and then include that in all the models you want to have the default behavior.
好的,根据其他回复的建议,我想出了这个解决方案:
中的 lib/clean_strings.rb
添加了 environment.rb
现在,每当我
这样做时,都会在保存之前清除它我的规则(它将去除空格,但不将其大写)。显然它需要更多的微调,但我认为这是为常见事物定义格式规则的好方法。这样我就不需要清理每个表单输入的内容,例如额外的空格,或者不知道大写锁定在哪里的人!
感谢大家的意见(全部投票)。
Ok based on the suggestions from other replies I came up with this solution:
lib/clean_strings.rb
in environment.rb addded
Now whenever I do
it cleans it before saving according to my rules ( it will strip whitespace, but not capitalize it ). Obviously it needs more fine tuning, but i think it's a good way to define format rules for commonplace things. This way I don't need to sanitize every and each form input for things like extra whitespaces, or people who don't know where the CAPS LOCK is !!!
Thank you all for your input ( all upvoted).