我有一篇文章模型,用于在我的网站页面上制作博客文章。我希望帖子的内容是HTML格式的,但是无法实现以HTML格式呈现。它只在博客页面上显示为纯文本。如果这有所作为,我正在使用Rails_Admin。
Ruby on Rails:博客文章未呈现在HTML中
posts
视图:
#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
模型:
class Post < ActiveRecord::Base
attr_accessible :content, :title, :author
has_many :comments
validates :title, :presence => true
validates :content, :presence => true
end
posts
控制器:
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
您没有显示任何CSS。没有CSS,它肯定会显得无聊和平淡。你确定这不是问题吗? – Amadan 2012-08-15 15:53:27
“纯文本”是什么意思?你只需要使它html_safe? – 2012-08-15 15:54:40