Ruby-on-rails Rails 中模型的辅助方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29141093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Helper methods for models in Rails
提问by at.
Is there a proper place for helper methods for models in Rails? There are helper methods for controllers and views, but I'm not sure where the best place to put model helper methods. Aside from adding a method to ActiveRecord::Base, which I'd prefer not to.
Rails 中模型的辅助方法是否有合适的位置?有用于控制器和视图的辅助方法,但我不确定放置模型辅助方法的最佳位置。除了向 中添加方法之外ActiveRecord::Base,我不希望这样做。
UPDATE: It seems Concerns make a lot of sense. Here's an example of what I want. Certain models can never be deleted, so I add a callback that always throws an exception:
更新:似乎担忧很有意义。这是我想要的一个例子。某些模型永远无法删除,所以我添加了一个总是抛出异常的回调:
before_destroy :nope
def nope
raise 'Deleting not allowed'
end
With concerns, I could do something like this?
带着顾虑,我可以做这样的事情吗?
class MyModel < ActiveRecord::Base
include Undeletable
end
module Undeletable
extend ActiveSupport::Concern
included do
before_destroy :nope
end
def nope
raise 'Deleting not allowed'
end
end
Is this the Rails way of doing this?
这是 Rails 的做法吗?
回答by Gagan Gami
If you want to use a helper_methodmy_helper_methodinside a model, you can write
如果你想在 a 中使用helper_method,你可以写my_helper_methodmodel
ApplicationController.helpers.my_helper_method
If you need a bit more flexibility, for example if you also need to override some methods, you can do this:
如果您需要更大的灵活性,例如如果您还需要覆盖某些方法,您可以这样做:
class HelperProxy < ActionView::Base
include ApplicationController.master_helper_module
def current_user
#let helpers act like we're a guest
nil
end
def self.instance
@instance ||= new
end
end
and then use with
然后使用
HelperProxy.instance.my_helper_method
If you have strong nerves, you can also try to include the ApplicationController.master_helper_moduledirectly into your model.
如果您有强烈的神经,您也可以尝试将ApplicationController.master_helper_module直接包含到您的model.
via : makandracards's post.
通过:makandracards 的帖子。
For your reference: http://railscasts.com/episodes/132-helpers-outside-views
供您参考:http: //railscasts.com/episodes/132-helpers-outside-views
回答by ReggieB
If what you are asking is where to put code that is shared across multiple models in rails 4.2, then the standard answer has to be to use Concerns: How to use concerns in Rails 4
如果您要问的是在 rails 4.2 中将跨多个模型共享的代码放在哪里,那么标准答案必须是使用关注点:如何在 Rails 4 中使用关注点
However, there are some good arguments (e.g. this) to just using standard rails module includes, and extends as marek-lipka suggests.
然而,有一些很好的论据(例如this)只使用标准的 rails 模块包含,并按照 marek-lipka 的建议扩展。
I would strongly recommend NOT using ApplicationController helper methods in a model, as you'll be importing a lot unnecessary baggage along with it. Doing so is usually a bad smell in my opinion, as it means you are not separating the MVC elements, and there is too much interdependency in your app.
我强烈建议不要在模型中使用 ApplicationController 辅助方法,因为您将同时导入很多不必要的包袱。在我看来,这样做通常很糟糕,因为这意味着您没有分离 MVC 元素,并且您的应用程序中有太多的相互依赖。
If you need to modify a model object by adding a method that is just used within a view, then have a look at decorators. For example https://github.com/drapergem/draper
如果您需要通过添加仅在视图中使用的方法来修改模型对象,请查看装饰器。例如https://github.com/drapergem/draper

