2012-08-15 78 views
2

我有一篇文章模型,用于在我的网站页面上制作博客文章。我希望帖子的内容是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 
+0

您没有显示任何CSS。没有CSS,它肯定会显得无聊和平淡。你确定这不是问题吗? – Amadan 2012-08-15 15:53:27

+0

“纯文本”是什么意思?你只需要使它html_safe? – 2012-08-15 15:54:40

回答

2

你的观点必须是这样的:

#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.html_safe 

= paginate @posts_list 

你也可以创建一个帮手:

def post_content(post) 
    RedCloth.new(post.content).to_html.html_safe 
end 

只需在您的视图中使用它。

+0

仍然没有得到任何html在视图中显示。看看页面源代码,我看到了html标记,但没有渲染。我添加了助手,并在视图中使用它。 – 2012-08-15 16:48:16

+0

它被继承的CSS覆盖。弄清楚了! – 2012-08-15 18:28:40

1

下创建的意见/职位的部分称为 '_post.html.haml'

现在,在您的文章查看,只需使用:

=render @posts_list 

你在 '_post.html.haml' 部分:

%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