我想我的 rails 应用程序中有一个工作版本的acts_as_commenting_with_threading,但似乎每条评论的正文都以奇怪的格式保存。如何删除视图中的格式,使其仅显示文本(而不显示格式)?例如,如果我输入文本“测试评论”,则评论的正文将保存为“---\nbody: test comment\n”。我尝试了 html_safe,但它没有用。
步骤.rb
class Step < ActiveRecord::Base
extend FriendlyId
acts_as_commentable
friendly_id :position
has_ancestry :orphan_strategy => :adopt
attr_accessible :description, :name, :position, :project_id, :images_attributes, :parent_id, :ancestry, :published_on
belongs_to :project
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images, :allow_destroy => :true
validates :name, :presence => true
end
评论控制器.rb
class CommentsController < ApplicationController
def create
@project = Project.find(params[:project_id])
@commentText = params[:comment]
@user = current_user
@comment = Comment.build_from(@project.steps.find(params[:step_id]), @user.id, @commentText)
respond_to do |format|
if @comment.save
format.html {redirect_to :back}
else
format.html { render :action => 'new' }
end
end
end
end
显示.html.erb:
<div class="stepComments">
<% if step.comment_threads.count >0 %>
<% step.comment_threads.each do |stepComment| %>
<% if stepComment.body.length>0 %>
<%= render :partial => 'comments', :locals => {:comment=> stepComment} %>
<% end %>
<br>
<% end %>
<% end %>
</div>
_comments.html.erb
<div class="comment">
<div class="userIcon">
<%= User.find(comment.user_id).username %>
<%= image_tag(User.where(:id=>comment.user_id).first.avatar_url(:thumb), :class=>"commentAvatar img-polaroid")%>
</div>
<div class="field">
<%= comment.body %>
</div>
</div>
这将打印:“---\nbody: test comment\n”