Rails 在视图之外使用 ActionView::Helper 方法

Sas*_*ash 1 ruby-on-rails

我创建了一个助手,我想用它来进行文本操作

module ApplicationHelper

  module TextHelper

    extend ActionView::Helpers

  end
end
Run Code Online (Sandbox Code Playgroud)

但是,当我ApplicationHelper::TextHelper.simple_format "foo"在 Rails 控制台中运行时,我得到

NoMethodError: undefined method `white_list_sanitizer' for Module:Class
Run Code Online (Sandbox Code Playgroud)

我还需要添加其他内容吗?

我已经看过https://github.com/rails/rails/issues/13837但它不起作用。

使用 Rails 4、Ruby 1.9.3

jer*_*ink 6

如果您在控制台中,您应该能够执行helper.simple_format('hi'). 该helper方法可在控制台中作为调用某些帮助器方法的方式使用。

使用自定义助手时:

# app/helpers/custom_helper.rb
module CustomHelper
  def custom_method(x)
    puts "Custom method #{x}"
  end
end

# from the console
helper.custom_method('hi')

# from the controller
class SomeController < ApplicationController

  def index
    view_context.custom_method('hi')
  end
end
Run Code Online (Sandbox Code Playgroud)