0

Railsアプリにacts_as_commenting_with_threadingの動作バージョンがあると思いますが、すべてのコメントの本文が奇妙なフォーマットで保存されているようです。ビューの書式設定を削除して、書式設定ではなくテキストのみを表示するにはどうすればよいですか? たとえば、「テスト コメント」というテキストを入力すると、コメントの本文は「---\n本文: テスト コメント\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

コメント_コントローラー.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"

4

3 に答える 3

0

文字列を手動で編集する以外に、それを行う方法がわかりませんでした。これは私が最終的に使用したものです:

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

これを行うための組み込み関数がないのは非常に奇妙に思えます...

于 2013-06-06T21:27:15.243 に答える