I have a posts model that I use to make blog posts on a page of my site. I’d like for the content of the post to be HTML formatted, but am having trouble getting it to actually render in HTML. It only shows up as plain text on the blog page. I am using Rails_Admin if that makes a difference.
posts view:
#blog_page
- @posts_list.each do |post|
%ul.post
%li= post.created_at.strftime("%B %d, %Y")
%li.title= post.title
%li.author
= 'By: '
= post.author
%li.content= RedCloth.new(post.content).to_html
= paginate @posts_list
post model:
class Post < ActiveRecord::Base
attr_accessible :content, :title, :author
has_many :comments
validates :title, :presence => true
validates :content, :presence => true
end
posts controller:
class PostsController < ApplicationController
before_filter :authenticate_user!, :only => [:new, :create]
# GET /posts
def index
@posts_list = Post.order("created_at DESC").page(params[:page]).per(5)
render 'blog'
end
# GET /posts/
def show
@post = Post.find(params[:id])
render 'show'
end
def new
if !current_user.admin?
redirect_to '/blog'
end
@post = Post.new
end
# POST /posts
def create
@post = Post.new(params[:post])
if current_user.admin?
respond_to do |format|
if @post.save
format.html { redirect_to('/posts', :notice => 'Post was successfully created.') }
format.json { render :json => @post, :status => :created, :location => @post }
else
flash[:notice] = 'Error creating post!<br/>'.html_safe
@post.errors.full_messages.each do |msg|
flash[:notice] << "<br/>".html_safe
flash[:notice] << msg
end
format.html { render :action => "new" }
format.json { render :json => @post.errors, :status => :unprocessable_entity }
end
end
else
redirect_to '/blog'
end
end
end
Your view has to be like this:
You could also create a helper for that:
And just use it in your view.