Skip to content

Examples

railsrocks edited this page Mar 28, 2016 · 9 revisions

Carpet has many different ways to render a model's field with Redcarpet. These examples will hopefully guide you when you use Carpet in your model. Before trying out the examples, make sure you have installed Carpet.


Render a field with Redcarpet's default renderer.

  class Post < ApplicationRecord
    redcarpetable :body
  end

By adding this line to your model, the body field can now accept markdown. To get the rendered markdown, use this code:

  somepost.rendered_body # Returns the body of the post as HTML

This will return the body of the post as HTML. If you were to use this in a view, it would look like this:

  <%= @post.rendered_body %>

Render multiple fields with Redcarpet's default renderer.

  class Post < ApplicatonRecord
    redcarpetable :body, :intro
  end

When this line is added to the model, it creates a method to show the rendered markdown for the body, and a method to show the rendered markdown for the intro. To view the rendered content, do this:

  somepost.rendered_body # Returns the body of the post as HTML
  somepost.rendered_intro # Returns the intro of the post as HTML

To get the HTML for these fields in a view, do this:

  <%= @post.rendered_body %> <!-- Render the post's body in the HTML document -->
  <%= @post.rendered_intro %> <!-- Render the post's into in the HTML document -->

Render a field with Redcarpet's default renderer, and provide options for the default renderer.

  class Post < ApplicationRecord
    redcarpetable :body, render_opts: {no_links: true}
  end

By adding this code to your model, the body field can now accept markdown, but will not render links. To get the rendered body, use this code:

  somepost.rendered_body # Render the post's body without links

This will return the body in HTML, however there will be no links in the HTML. The same thing will happen when the body of a post is rendered in a view. To render the body in a view, do this as you normally would:

  <%= @post.rendered_body %> <!-- Render the post's body without links in the HTML document -->

Render multiple fields with Redcarpet's default renderer, and provide options for the default renderer.

  class Post < ApplicationRecord
     redcarpetable :body, :intro, render_opts: {no_links: true}
  end

This will generate two methods that generate the rendered body and intro, however they will not render links. To call these methods, do this:

  somepost.rendered_body # Returns the rendered body without links
  somepost.rendered_intro # Returns the rendered intro without links

In a view, do this:

  <%= @post.body %> <!-- Render the post's body in the HTML document -->
  <%= @post.rendered_intro %> <!-- Render the post's intro in the HTML document ->

Render multiple fields in a model with Redcarpet's default renderer, and provide different options to the default renderer for each field.

  class Post < ApplicationRecord
    redcarpetable :body, render_opts: {no_links: true}
    redcarpetable :intro, render_opts: {generate_toc_data: false}
  end

This will generate a method to render the body and intro, and that method will call the default Redcarpet renderer with the options specified. To get the rendered content, do this:

  somepost.rendered_body # Returns the rendered body without links
  somepost.rendered_intro # Returns the rendered intro without the table of contents data

In a view, to render the content of a field, do this:

  <%= @post.rendered_body %> <!-- Render the post's body in the HTML document -->
  <%= @post.rendered_intro %> <!-- Render the post's intro in the HTML document -->
Clone this wiki locally