在 Rails 3 中保存之前操作表单数据的最佳实践位置是什么?

发布于 2024-10-22 06:10:15 字数 522 浏览 1 评论 0原文

从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

§普罗旺斯的薰衣草 2024-10-29 06:10:15

最简单的地方就是在你的模型中。如果您觉得更适合,我建议使用 before_save 甚至 before_validation 。像这样的事情就可以解决问题:

before_save :upcase_content

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

此外,如果您想允许具体情况的例外,您可以向模型添加 attr_accessor 。

class MyModel < ActiveRecord::Base
attr_accessor :dont_upcase

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

然后当您创建模型时将访问器设置为 true

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

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:

before_save :upcase_content

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

Additionally if you wanted to allow for exceptions of a case by case basis you could add an attr_accessor to your model.

class MyModel < ActiveRecord::Base
attr_accessor :dont_upcase

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

then when you create a model set the accessor to true

@model = Model.new(:brand_name => utorrent)
@model.dont_upcase = true
@model.save!
陈年往事 2024-10-29 06:10:15

放置它的最佳位置是在您的模型中,这样您就有一个胖模型和一个瘦控制器,这是一件“好事”。

如果您希望所有模型都可以使用此功能,我的建议是使用包含共享功能的模块,然后将其包含在您想要具有默认行为的所有模型中。

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.

夏尔 2024-10-29 06:10:15

好的,根据其他回复的建议,我想出了这个解决方案:

中的 lib/clean_strings.rb

module ActiveRecord
  class Base
    attr_accessor :dont_capitlize, :dont_strip

    before_save :_capitalize_strings, :unless => :dont_capitlize
    before_save :_strip_whitespaces,  :unless => :dont_strip

    def _capitalize_strings
      self.attributes.each_pair do |key, value|
        self[key] = value.capitalize if value.respond_to?('capitalize')
      end
    end


    def _strip_whitespaces
      self.attributes.each_pair do |key, value|
        self[key] = value.strip if value.respond_to?('strip')
      end
    end

  end
end

添加了 environment.rb

require "clean_strings"

现在,每当我

@a.dont_capitalize = true
@a.save!

这样做时,都会在保存之前清除它我的规则(它将去除空格,但不将其大写)。显然它需要更多的微调,但我认为这是为常见事物定义格式规则的好方法。这样我就不需要清理每个表单输入的内容,例如额外的空格,或者不知道大写锁定在哪里的人!

感谢大家的意见(全部投票)。

Ok based on the suggestions from other replies I came up with this solution:

lib/clean_strings.rb

module ActiveRecord
  class Base
    attr_accessor :dont_capitlize, :dont_strip

    before_save :_capitalize_strings, :unless => :dont_capitlize
    before_save :_strip_whitespaces,  :unless => :dont_strip

    def _capitalize_strings
      self.attributes.each_pair do |key, value|
        self[key] = value.capitalize if value.respond_to?('capitalize')
      end
    end


    def _strip_whitespaces
      self.attributes.each_pair do |key, value|
        self[key] = value.strip if value.respond_to?('strip')
      end
    end

  end
end

in environment.rb addded

require "clean_strings"

Now whenever I do

@a.dont_capitalize = true
@a.save!

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文