rails截取字符串,点击more的时候显示

页面中的description比较长, 想截取一部分,后面的用more代替,点击more的时候显示全部。这种情况比较长见。代码如下:

在application.helper中写个方法:

  def truncate_with_more (text, cutoff, id)
        if text.length > cutoff
          result = text[0, cutoff]
          result += "<span id='text_more_link_#{id}'>&hellip;"
                result += "<a href='#' onclick='$(\"text_more_#{id}\").show(); $(\"text_more_link_#{id}\").hide(); return false;'>"
                result += "more</a></span>"
                result += "<span id='text_more_#{id}' style='display: none;'>"
                result += text[cutoff, text.length]
              result += "</span>"
        else
              result = text
        end
      
        result
  end

页面中调用:
<%= truncate_with_more post.description, 200, "post_#{post.id}" %>

blog.csdn.net/xieqibao/archive/2007/11/02/1862796.aspx

你可能感兴趣的:(.net,Blog,Rails)