我认为我的rails应用程序中有一个acts_as_commenting_with_threading的工作版本,但似乎每个评论的主体都是用奇怪的格式保存的。如何删除视图中的格式,使其只显示文本(而不是格式)?例如,如果我键入文本"test注释“,注释的正文将保存为”我试过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
endcomments_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
endshow.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>打印:
发布于 2013-06-06 20:55:58
rails助手格式化将使用格式规则进行打印,这样您就可以得到文本。
例如,<% simple_format(comment.body) %>
发布于 2013-06-06 21:27:15
除了手动编辑字符串之外,我想不出一种方法来完成它。这就是我最后使用的:
<%= comment.body.slice((comment.body.index(' ')+1..comment.body.length)) %>似乎很奇怪,没有内置的功能来做这件事.
发布于 2013-06-10 15:03:50
最后,这是一个非常简单的解决方案;我一直在错误地调用参数。它应该是:
@commentText = params[:comment][:body]https://stackoverflow.com/questions/16971130
复制相似问题