[英]Rails polymorphic comments dumping object in view - how do I stop this?
I am learning Rails and recently implemented comments as a polymophic association. 我正在学习Rails,并且最近将注释实现为多语言协会。 Everything works fine except along with the comments being rendered in my view I am also getting the raw object code dumped into the view and rendering below the comments.
一切正常,除了在我的视图中呈现的注释之外,我还将原始对象代码转储到视图中并在注释下方进行呈现。 I have spent hours trying to find the solution but cannot.
我已经花费了数小时试图找到解决方案,但是没有找到。 Is there ayone out there that can help or point me in the right direction?
有没有人可以帮助或指出正确的方向? Ruby version: 2.3.1p112, Rails version: 5.1.4 Chromium: 63.0.3239.132 (Official Build) Built on Ubuntu , running on Ubuntu 16.04 (64-bit)
Ruby版本:2.3.1p112,Rails版本:5.1.4 Chromium:63.0.3239.132(官方内部版本)基于Ubuntu构建,在Ubuntu 16.04(64位)上运行
Screenshot: screenshot of view page 屏幕截图: 查看页面的屏幕截图
Thanks! 谢谢!
Models: 楷模:
/app/models/pin.rb /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 /app/models/comment.rb
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
belongs_to :user
end
Controllers: 控制器:
/app/controllers/pins_controller.rb /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 /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 /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
Views: 观看次数:
/app/views/show.html.haml /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: app / view / comments / _comments.html.haml:
#Comments
%h4 Comments..
= @pin.comments.each do |comment| #- problem here
.well
= comment.body
Routes: 路线:
/config/routes.rb /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
Your are getting a raw object because you are using =
in your iteration block which will display the @pin.comments
object. 您得到的是原始对象,因为在迭代块中使用
=
,它将显示@pin.comments
对象。 Changing 改变中
= @pin.comments.each do |comment|
to 至
- @pin.comments.each do |comment|
will do the trick. 会成功的 Hope this helps.
希望这可以帮助。
modify following code 修改以下代码
#Comments
%h4 Comments..
= @pin.comments.each do |comment| #- problem here
.well
= comment.body
as 如
#Comments
%h4 Comments..
- @pin.comments.each do |comment|
.well
= comment.body
you are using haml template engine so, An equals sign, =
, will output the result of the code. 您正在使用haml模板引擎,因此,等号
=
将会输出代码的结果。 A hyphen, -
, will run the code but not output the result. 连字符
-
将运行代码,但不输出结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.