Rails,未定义的“原始”方法

Sas*_*sha 0 ruby-on-rails undefined

我正在关注迈克尔·哈特尔的书,并一直在做一个我似乎无法弄清楚的可选练习。

我有一个帮手,叫

module MicropostsHelper

  def wrap(content)
    sanitize(raw(content.split.map{ |s| wrap_long_string(s) }.join(' ')))
  end
  private
    def wrap_long_string(text, max_width = 30)
      zero_width_space = "​"
      regex = /.{1,#{max_width}}/
      (text.length < max_width) ? text : 
                                  text.scan(regex).join(zero_width_space)
    end
end
Run Code Online (Sandbox Code Playgroud)

我正在尝试包装应该是用户输入中的字符串的内容。在我的控制器中,

def create
    flash[:notice] = "[][] is...", params[:micropost][:content]
    cleaned = wrap(params[:micropost][:content])
    @micropost = current_user.microposts.build(cleaned)
    ...
Run Code Online (Sandbox Code Playgroud)

但是我不断收到未定义的方法“原始”。有人可以解释为什么吗?

有没有显示方法示例的 api?

Sai*_*ram 5

您可以在 html 视图中使用string.html_safer 'string'

  • raw 是一个助手,您需要将其包含到您的模型中,或者按照 Sairam 的建议使用 html_safe 。 (3认同)