Rails 3传入选项语法



Avi Tzurel编写了这个帮助器,以获得更好的simple_format:

  def simple_format_no_tags(text, html_options = {}, options = {})
    text = '' if text.nil?
    text = smart_truncate(text, options[:truncate]) if options[:truncate].present?
    text = sanitize(text) unless options[:sanitize] == false
    text = text.to_str
    text.gsub!(/rn?/, "n")                    # rn and r -> n
    text.gsub!(/([^n]n)(?=[^n])/, '1<br />') # 1 newline   -> br
    text.html_safe
  end

在我看来是这样的:

<%= simple_format_no_tags(article.text) %>

我是编程和rails的新手-传递截断144个字符的选项的语法是什么?

simple_format_no_tags(article.text,{},{:truncate => 144})

simple_format_no_tags方法为truncate键查找options参数。因为options是第三个参数,如果你没有任何html选项要传入,你就必须传递空哈希值,或者为第二个参数传递nil。

在这里找到这个实现。看看这是否适合你

def smart_truncate(text, char_limit)
  size = 0
  text.split.reject do |token|
    size += (token.size + 1)
    size > char_limit
  end.join(" ") + (text.size >= char_limit ? " ..." : "" )
end

最新更新