Rails 视图中的多态注释转储对象 - 我该如何阻止它?

Rails polymorphic comments dumping object in view - how do I stop this?

我正在学习 Rails 并且最近将评论实现为多态关联。一切正常,除了在我的视图中呈现的评论之外,我还将原始对象代码转储到视图中并在评论下方呈现。 我花了几个小时试图找到解决方案,但没有成功。有没有人可以帮助或指出正确的方向? Ruby 版本:2.3.1p112,Rails 版本:5.1.4 Chromium:63.0.3239.132(官方构建)基于 Ubuntu 构建,运行 在 Ubuntu 16.04 上构建(64 位)

截图: screenshot of view page

谢谢!

型号:

/app/models/pin.rb

class Pin < ApplicationRecord
    acts_as_votable
    belongs_to :user
    has_many :comments, as: :commentable

    has_attached_file :image, styles: { medium: "300x300>" }, 
    default_url: "/images/:style/missing.png"
    validates_attachment_content_type :image, content_type: 
    /\Aimage\/.*\z/
end

/app/models/comment.rb

class Comment < ApplicationRecord
    belongs_to :commentable, polymorphic: true
    belongs_to :user
end

控制器:

/app/controllers/pins_controller.rb

class PinsController < ApplicationController
    before_action :find_pin, only: [:show, :edit, :update, :destroy, :upvote]
    before_action :authenticate_user!, except: [:index, :show]

    def index
        @pins = Pin.all.order("created_at DESC")
    end

    def show
    end

    def new
        @pin = current_user.pins.build
    end

    def create
        @pin = current_user.pins.build(pin_params)

        if @pin.save
            redirect_to @pin, notice: "Successfully created new Pin."
        else
            render 'new'
        end
    end

    def edit
    end

    def update
        if @pin.update(pin_params)
            redirect_to @pin, notice: "Pin was Successfully updated!"
        else
            render 'edit'
        end
    end

    def destroy
        @pin.destroy
        redirect_to root_path
    end

    def upvote
        @pin.upvote_by current_user
        redirect_back fallback_location: root_path
    end


    private

    def pin_params
        params.require(:pin).permit(:title, :description, :image)
    end

    def find_pin
        @pin = Pin.find(params[:id])
    end

end

/app/controllers/comments_controller.rb

class CommentsController < ApplicationController
    before_action :authenticate_user!

    def create
        @comment = @commentable.comments.new comment_params
        @comment.user = current_user
        @comment.save
            redirect_to @commentable, notice: "Your comment was successfully posted."
    end


    private

    def comment_params
        params.require(:comment).permit(:body)
    end

end

/app/controllers/pins/comments_controller.rb

class Pins::CommentsController < CommentsController
    before_action :set_commentable

    private
        def set_commentable
            @commentable = Pin.find(params[:pin_id])
        end


end 

观看次数:

/app/views/show.html.haml

#pin_show.row
    .col-md-8.col-md-offset-2
        .panel.panel-default
            .panel-heading.pin_image
                =image_tag @pin.image.url
            .panel-body
                %h1= @pin.title
                %p.description= @pin.description

                = render partial: "comments/comments" , locals: {commentable: @pin} 
                = render partial: "comments/form", locals: {commentable: @pin}

            .panel-footer
                .row
                    .col-md-6
                        %p.user
                        Pin submitted by
                        = @pin.user.email
                    .col-md-6
                        .btn-group.pull-right
                            = link_to like_pin_path(@pin), method: :put, class: "btn btn-default" do
                                %span.glyphicon.glyphicon-heart
                                    = @pin.get_upvotes.size
                            - if user_signed_in?
                                = link_to "Edit", edit_pin_path, class: "btn btn-default"
                                %button.btn.btn-danger{"data-target" => "#exampleModal#{@pin.id}", "data-toggle" => "modal", type: "button"} Delete

                                =render 'modal'

app/view/comments/_comments.html.haml:

#Comments
    %h4 Comments..
    = @pin.comments.each do |comment|   #- problem here
        .well
            = comment.body

路线:

/config/routes.rb

Rails.application.routes.draw do
  devise_for :users
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

  resources :pins do
    member do
        put "like", to: "pins#upvote"
    end
    resources :comments, module: :pins
  end


  root "pins#index"
end

您正在获取原始对象,因为您在将显示 @pin.comments 对象的迭代块中使用 =。改变

= @pin.comments.each do |comment|

- @pin.comments.each do |comment|

会成功的。希望这有帮助。

修改以下代码

#Comments
    %h4 Comments..
    = @pin.comments.each do |comment|   #- problem here
        .well
            = comment.body

作为

#Comments
    %h4 Comments..
    - @pin.comments.each do |comment|
        .well
            = comment.body

您正在使用 haml 模板引擎,因此,等号 = 将输出代码的结果。连字符 - 将 运行 代码但不输出结果。