Module ActionView::Helpers::TextHelper
In: lib/action_view/helpers/text_helper.rb

Provides a set of methods for working with text strings that can help unburden the level of inline Ruby code in the templates. In the example below we iterate over a collection of posts provided to the template and print each title after making sure it doesn‘t run longer than 20 characters:

  <% for post in @posts %>
    Title: <%= truncate(post.title, 20) %>
  <% end %>

Methods

Constants

VERBOTEN_TAGS = %w(form script) unless defined?(VERBOTEN_TAGS)
VERBOTEN_ATTRS = /^on/i unless defined?(VERBOTEN_ATTRS)
AUTO_LINK_RE = / ( # leading text <\w+.*?>| # leading HTML tag, or [^=!:'"\/]| # leading punctuation, or ^ # beginning of line ) ( (?:http[s]?:\/\/)| # protocol spec, or (?:www\.) # www.* ) ( ([\w]+:?[=?&\/.-]?)* # url segment \w+[\/]? # url tail (?:\#\w*)? # trailing anchor ) ([[:punct:]]|\s|<|$) # trailing text /x unless const_defined?(:AUTO_LINK_RE)

Public Instance methods

Turns all urls and email addresses into clickable links. The link parameter can limit what should be linked. Options are :all (default), :email_addresses, and :urls.

Example:

  auto_link("Go to http://www.rubyonrails.com and say hello to david@loudthinking.com") =>
    Go to <a href="http://www.rubyonrails.com">http://www.rubyonrails.com</a> and
    say hello to <a href="mailto:david@loudthinking.com">david@loudthinking.com</a>

If a block is given, each url and email address is yielded and the result is used as the link text. Example:

  auto_link(post.body, :all, :target => '_blank') do |text|
    truncate(text, 15)
  end

The regular puts and print are outlawed in eRuby. It‘s recommended to use the <%= "hello" %> form instead of print "hello". If you absolutely must use a method-based output, you can use concat. It‘s used like this: <% concat "hello", binding %>. Notice that it doesn‘t have an equal sign in front. Using <%= concat "hello" %> would result in a double hello.

Returns a Cycle object whose to_s value cycles through items of an array every time it is called. This can be used to alternate classes for table rows:

  <%- for item in @items do -%>
    <tr class="<%= cycle("even", "odd") %>">
      ... use item ...
    </tr>
  <%- end -%>

You can use named cycles to prevent clashes in nested loops. You‘ll have to reset the inner cycle, manually:

  <%- for item in @items do -%>
    <tr class="<%= cycle("even", "odd", :name => "row_class")
      <td>
        <%- for value in item.values do -%>
          <span style="color:'<%= cycle("red", "green", "blue"
                                        :name => "colors") %>'">
            item
          </span>
        <%- end -%>
        <%- reset_cycle("colors") -%>
      </td>
   </tr>
 <%- end -%>

Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius. If the phrase isn‘t found, nil is returned. Ex:

  excerpt("hello my world", "my", 3) => "...lo my wo..."

Highlights the phrase where it is found in the text by surrounding it like <strong class="highlight">I‘m a highlight phrase</strong>. The highlighter can be specialized by passing highlighter as single-quoted string with \1 where the phrase is supposed to be inserted. N.B.: The phrase is sanitized to include only letters, digits, and spaces before use.

Returns the text with all the Markdown codes turned into HTML-tags. This method is only available if BlueCloth can be required.

Attempts to pluralize the singular word unless count is 1. See source for pluralization rules.

Resets a cycle so that it starts from the first element in the array the next time it is used.

Sanitizes the given HTML by making form and script tags into regular text, and removing all "onxxx" attributes (so that arbitrary Javascript cannot be executed). Also removes href attributes that start with "javascript:".

Returns the sanitized text.

Returns text transformed into HTML using very simple formatting rules Surrounds paragraphs with <p> tags, and converts line breaks into <br/> Two consecutive newlines(\n\n) are considered as a paragraph, one newline (\n) is considered a linebreak, three or more consecutive newlines are turned into two newlines

Turns all links into words, like "<a href="something">else</a>" to "else".

Strips all HTML tags from the input, including comments. This uses the html-scanner tokenizer and so it‘s HTML parsing ability is limited by that of html-scanner.

Returns the tag free text.

Returns the text with all the Textile codes turned into HTML-tags. This method is only available if RedCloth can be required.

Returns the text with all the Textile codes turned into HTML-tags, but without the regular bounding <p> tag. This method is only available if RedCloth can be required.

Truncates text to the length of length and replaces the last three characters with the truncate_string if the text is longer than length.

Word wrap long lines to line_width.

[Validate]