[英]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位)上運行
謝謝!
楷模:
/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模板引擎,因此,等號=
將會輸出代碼的結果。 連字符-
將運行代碼,但不輸出結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.