Rails 3传入选项语法

时间:2014-03-12 21:13:13

标签: ruby-on-rails

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!(/\r\n?/, "\n")                    # \r\n 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个字符的选项的语法是什么?

1 个答案:

答案 0 :(得分:1)

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

simple_format_no_tags方法查看options密钥的truncate参数。由于options是第三个参数,如果没有任何html选项可以传入,则必须为第二个参数传递空哈希或nil。

找到此实施here。看看这是否适合你

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