【问题标题】:acts as commentable: comment body formatting充当可评论:评论正文格式
【发布时间】:2013-06-06 20:00:57
【问题描述】:

我认为我的 rails 应用程序中有一个可用的acts_as_commenting_with_threading 版本,但似乎每条评论的正文都以奇怪的格式保存。如何删除视图中的格式,使其仅显示文本(而不显示格式)?例如,如果我键入文本“测试评论”,则评论的正文将保存为“---\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

cmets_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>

_cmets.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”

【问题讨论】:

    标签: ruby-on-rails acts-as-commentable


    【解决方案1】:

    rails helper simple_format 将使用格式规则打印,因此您将只得到文本。

    例如,&lt;% simple_format(comment.body) %&gt;

    【讨论】:

    • 感谢您的帮助。我试过了,但它仍然存在格式问题。
    • 你在保存之前试过squishing字符串吗?
    • 我试过squishing,但它并没有删除body开头的奇怪的“--body”标签。
    【解决方案2】:

    除了手动编辑字符串之外,我想不出办法。这就是我最终使用的:

    <%= comment.body.slice((comment.body.index(' ')+1..comment.body.length)) %>
    

    似乎很奇怪,没有一些内置函数可以做到这一点......

    【讨论】:

      【解决方案3】:

      它最终成为一个非常简单的解决方案;我一直在错误地调用参数。应该是:

      @commentText = params[:comment][:body]
      

      【讨论】:

        猜你喜欢
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        • 1970-01-01
        • 2011-07-28
        • 2011-04-15
        • 2018-05-28
        • 2013-07-10
        相关资源
        最近更新 更多