-
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
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 %>