Ruby on rails 在模型中使用助手:如何包含助手依赖项?

Ruby on rails 在模型中使用助手:如何包含助手依赖项?,ruby-on-rails,ruby,activerecord,model,Ruby On Rails,Ruby,Activerecord,Model,我正在编写一个模型来处理来自文本区域的用户输入。按照来自的建议,在保存到数据库之前,我将使用before_validate回调清除模型中的输入 我的模型的相关部分如下所示: include ActionView::Helpers::SanitizeHelper class Post < ActiveRecord::Base { before_validation :clean_input ... protected def clean_input self.

我正在编写一个模型来处理来自文本区域的用户输入。按照来自的建议,在保存到数据库之前,我将使用before_validate回调清除模型中的输入

我的模型的相关部分如下所示:

include ActionView::Helpers::SanitizeHelper

class Post < ActiveRecord::Base {
  before_validation :clean_input

  ...

  protected

  def clean_input
    self.input = sanitize(self.input, :tags => %w(b i u))
  end
end
包含ActionView::Helpers::SanitizeHelper
类Post%w(biu))
结束
结束
不用说,这不起作用。当我尝试保存一篇新文章时,我会出现以下错误

undefined method `white_list_sanitizer' for #<Class:0xdeadbeef>
未定义的方法“白名单消毒液”#

显然,SanitizeHelper创建了一个HTML::WhiteListSanitizer的实例,但当我将其混合到模型中时,它找不到HTML::WhiteListSanitizer。为什么?我可以对此做些什么来修复它?

只需更改第一行,如下所示:

include ActionView::Helpers
这将使它起作用

更新:对于Rails 3,请使用:

ActionController::Base.helpers.sanitize(str)

归功于

这只提供了helper方法,而没有将每个ActionView::Helpers方法加载到模型中的副作用:

ActionController::Base.helpers.sanitize(str)

要从您自己的控制器访问帮助程序,只需使用:

OrdersController.helpers.order_number(@order)

我不推荐这些方法中的任何一种,而是把它放在它自己的名称空间中

class Post < ActiveRecord::Base
  def clean_input
    self.input = Helpers.sanitize(self.input, :tags => %w(b i u))
  end

  module Helpers
    extend ActionView::Helpers::SanitizeHelper
  end
end
class Post%w(biu))
结束
模块助手
扩展ActionView::Helpers::SanitizeHelper
结束
结束

这对我来说效果更好:

简单:

ApplicationController.helpers.my_helper_method
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
前进:

ApplicationController.helpers.my_helper_method
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
class HelperProxy

来源:

如果要在模型中使用
my\u helper\u方法
,可以编写:

ApplicationController.helpers.my_helper_method

我自己说得再好不过了谢谢。我通过将include移到类定义内部来实现它。这样我得到的
堆栈级别太深了
。它是在一个before_save方法中。请不要将视图层问题与活动记录模型混为一谈。这是一种糟糕的做法。更好的方法是将独立的输入数据放在nitizer对象,并从中检索“干净”属性。这是一个非常糟糕的解决方案,应该像fire一样避免。Rails基于MVC(模型视图控制器)帮助程序出现在视图中的框架部分,因此您不应将视图帮助程序方法与模型混合使用。对于像我这样的速度较慢的人,您不需要包含任何内容,只需使用ActionController::Base.helpers.sanitize(“在要清理的字符串上”)谢谢你,在Rails 2.3.14中工作,而接受的答案没有。我向应用程序\u助手添加了一个方法,但我无法使用ActionController::Base.helpers从模型访问它。使用Rails 3.0.3的我的\u方法(选项)?只使用
ApplicationController.helpers.order\u编号(@order)
。这意味着
订单号
位于
订单助手
@rowanu他说的是“访问(来自您自己控制器的助手)”,而不是(访问您自己控制器的助手)”在Rails 3和Rails 4中,
ApplicationController.master\u helper\u模块
不再存在了。不过,
ApplicationController.helpers
是一个不错的模块。我投了赞成票(简单选项)因为它适合我的需要-我只需要一个助手,它使用ApplicationController中的before筛选器保存的信息,所以在我的例子中,显式关联是一个提示,提示存在耦合。[用例是多域应用程序,它通过模型通知程序发出电子邮件,带有返回应用程序的url链接-此url根据web请求的域而变化]