-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
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.
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 %>
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 -->
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
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 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 ->