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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/489641/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:55:51  来源:igfitidea点击:

Using helpers in model: how do I include helper dependencies?

ruby-on-railsrubyactiverecordmodel

提问by O. Frabjous-Dey

I'm writing a model that handles user input from a text area. Following the advice from http://blog.caboo.se/articles/2008/8/25/sanitize-your-users-html-input, I'm cleaning up the input in the model before saving to database, using the before_validate callback.

我正在编写一个模型来处理来自文本区域的用户输入。按照http://blog.caboo.se/articles/2008/8/25/sanitize-your-users-html-input的建议,我在保存到数据库之前清理模型中的输入,使用 before_validate打回来。

The relevant parts of my model look like this:

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

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

Needless to say, this doesn't work. I get the following error when I try and save a new Post.

不用说,这行不通。当我尝试保存新帖子时出现以下错误。

undefined method `white_list_sanitizer' for #<Class:0xdeadbeef>

Apparently, SanitizeHelper creates an instance of HTML::WhiteListSanitizer, but when I mix it into my model it can't find HTML::WhiteListSanitizer. Why? What can I do about this to fix it?

显然,SanitizeHelper 创建了一个 HTML::WhiteListSanitizer 实例,但是当我将它混合到我的模型中时,它找不到 HTML::WhiteListSanitizer。为什么?我能做些什么来解决这个问题?

回答by lornc

This gives you just the helper method without the side effects of loading every ActionView::Helpers method into your model:

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

ActionController::Base.helpers.sanitize(str)

回答by Alfreddd

Just change the first line as follows :

只需按如下方式更改第一行:

include ActionView::Helpers

that will make it works.

这将使它起作用。

UPDATE:For Rails 3 use:

更新:对于 Rails 3 使用:

ActionController::Base.helpers.sanitize(str)

Credit goes to lornc's answer

幸得lornc的答案

回答by skozz

This works better for me:

这对我来说效果更好:

Simple:

简单的:

ApplicationController.helpers.my_helper_method

Advance:

进步:

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

Source: http://makandracards.com/makandra/1307-how-to-use-helper-methods-inside-a-model

资料来源:http: //makandracards.com/makandra/1307-how-to-use-helper-methods-inside-a-model

回答by Tarmo

To access helpers from your own controllers, just use:

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

OrdersController.helpers.order_number(@order)

回答by axsuul

I wouldn't recommend any of these methods. Instead, put it within its own namespace.

我不会推荐任何这些方法。相反,将它放在自己的命名空间中。

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

回答by Atchyut Nagabhairava

If you want to use a the my_helper_methodinside a model, you can write:

如果要my_helper_method在模型内部使用,可以这样写:

ApplicationController.helpers.my_helper_method