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

时间:2018-02-03 19:30:59

标签: ruby-on-rails polymorphism comments

我正在学习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'

应用程序/视图/评论/ _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

2 个答案:

答案 0 :(得分:0)

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

= @pin.comments.each do |comment|

- @pin.comments.each do |comment|

会做到这一点。希望这会有所帮助。

答案 1 :(得分:0)

修改以下代码

#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模板引擎,因此,等号,=将输出代码的结果。连字符-将运行代码但不输出结果。