我认为我的rails应用程序中有一个工作版本的acts_as_commenting_with_threading,但似乎每条评论的正文都以奇怪的格式保存。如何删除视图中的格式,使其仅显示文本(而不是格式)?例如,如果我键入文本“test comment”,则注释的正文将保存为“--- \ nbody:test comment \ n”。我试过html_safe,但它不起作用。
step.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
comments_controller.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
show.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”
答案 0 :(得分:0)
使用格式规则打印rails帮助器simple_format,这样您就可以获得文本。
例如,<% simple_format(comment.body) %>
答案 1 :(得分:0)
除了手动编辑字符串之外,我无法找到方法。这就是我最终使用的:
<%= comment.body.slice((comment.body.index(' ')+1..comment.body.length)) %>
似乎很奇怪,没有一些内置功能可以做到这一点...
答案 2 :(得分:0)
它最终成为一个非常简单的解决方案;我一直在错误地调用参数。应该是:
@commentText = params[:comment][:body]