【问题标题】:Rails 4: comment partial not renderingRails 4:评论部分不呈现
【发布时间】:2017-03-10 04:22:21
【问题描述】:

尝试显示照片的 cmets,照片属于个人资料。 html 不呈现。

相关代码:

routes.rb

  resources :profiles do
    resources :photos do
      resources :comments do
        resources :comments
      end
    end
  end

cmets/_comment.html.haml

= comments.each do |comment|
  %li
    = comment.body 
    \-
    %small Submitted 
    = #{time_ago_in_words(comment.created_at)}

    = semantic_form_for [@profile, @photo, comment, Comment.new] do |f|
      = f.inputs do
        = f.input :body, placeholder: "Add a Reply"
        %br/
      = f.actions do
        = f.action :submit, :as => :input, label: "Reply"
    %ul
      - render partial: 'comments/comment', locals: {comments: comment.comments}

models/photo.rb

class Photo < ActiveRecord::Base
  belongs_to :profile
  has_many :comments, as: :commentable, :dependent => :destroy
end

models/comment.rb

class Comment < ActiveRecord::Base
  belongs_to :profile
  belongs_to :commentable, polymorphic: true
  has_many :comments, as: :commentable, :dependent => :destroy
end

models/profile.rb

class Profile < ActiveRecord::Base
  belongs_to :user
  has_many :photos, :dependent => :destroy
  has_many :comments, :dependent => :destroy
end

app/controllers/phtos_controller.rb

class PhotosController < ApplicationController
  before_action :set_photo, only: [:show, :edit, :update, :destroy]
  before_action :set_profile
  load_and_authorize_resource

  def index
    @photos = Photo.where(:profile => @profile)
  end

  def show
  end

  def new
    @photo = Photo.new(:profile => @profile)
  end

  def edit
  end

  def create
    @photo = Photo.new(photo_params.merge(:profile_id => @profile.id))

    respond_to do |format|
      if @photo.save
        format.html { redirect_to [@profile, @photo], notice: 'Photo was successfully created.' }
        format.json { render :show, status: :created, location: @photo }
      else
        format.html { render :new }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @photo.update(photo_params)
        format.html { redirect_to [@profile, @photo], notice: 'Photo was successfully updated.' }
        format.json { render :show, status: :ok, location: @photo }
      else
        format.html { render :edit }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @photo.destroy
    respond_to do |format|
      format.html { redirect_to profile_photos_url, notice: 'Photo was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_photo
    @photo = Photo.find(params[:id])
  end

  def set_profile
    @profile = Profile.find(params[:profile_id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def photo_params
    params.require(:photo).permit(:description, :attachment)
  end
end

/app/views/photos/show.html.haml

= render partial: "layouts/sub_header", locals: {heading:    @photo.profile.name + "'s", sub_heading: "photo", current_bread_crumb: @photo.profile.name + "'s photo", include_crumbs: true}
/ Intro Content
.row
  .col-md-6
    = image_tag @photo.attachment.url(:large), :class => "img-responsive"
  .col-md-6
    %p
      %h2 About this photo...
      = simple_format(@photo.description)
      ,
 / /.row

 %h3 Comments

 = semantic_form_for [@profile, @photo, Comment.new] do |f|
   = f.inputs do
     = f.input :body, :as => :text
   = f.actions do
     = f.action :submit, :as => :input

 %ul
   - render partial: 'comments/comment', locals: {comments: @photo.comments}

 - if current_user == @profile.user
   = link_to 'Edit', edit_profile_photo_path(@profile, @photo)
   |
  = link_to 'Back', :back

正在将数据插入数据库(profile_id 除外,但我会将其保存到另一篇文章中)。我手动更新了 db 中的 profile_id 以查看它是否只是数据完整性问题,仍然没有。

我尝试在 routes.rb 中移动资源,我尝试重构视图以直接加载集合而不使用部分,似乎没有任何效果。

此外,如果我注释掉部分并使用此代码,我确实会在页面上看到评论正文,所以这肯定是我在调用部分或部分本身时做错了。

%ul
  - @photo.comments.each do |comment|
    = comment.body

我似乎无法破解这个,我知道这不是魔法,但我显然没有看到任何东西。

感谢您的帮助!

【问题讨论】:

  • 您如何确定 cmets 部分不呈现?您是否尝试在第一行之前添加一些 html 以进行仔细检查?
  • 我尝试在部分中使用 puts comment.body.inspect 行,但从未打印出来。
  • puts 转到可能不可见或在明显位置的标准输出。您可以做以下两件事之一,使用记录器- logger.debug('Rendering comments partial'),然后查看您的日志文件。添加一些明显的 html,例如:%div{style:'height:30px;width:200:px;background:red'} Rendered comments
  • 另外,一旦你解决了这个问题,我建议你重组你的部分以只呈现一条评论,并在此处查看第 3.4.5 节渲染集合guides.rubyonrails.org/…
  • 还有两件快速的事情(1)我怀疑你会发现你的问题是你没有 cmets,你可以通过使用 rails 控制台或记录 cmets 的计数来测试这个(2)在在您的路线文件中,您在另一个 cmets 资源中有一个 cmets 资源。这不太可能是正确的

标签: ruby ruby-on-rails-4 haml


【解决方案1】:

show.html.haml 更改为:

%ul
  - render 'comments/comment', locals: {comments: @photo.comments}

原因是,您不能在视图中渲染视图,因此上面假设您正在 cmets 文件夹中查找名为 _comment.html.haml 的部分内容。

【讨论】:

  • partial: 的目的是什么:那么如果不是在视图中渲染它呢?
  • 是的,这就是局部视图的目的,我的意思是您不能在视图中呈现完整视图,因此 Rails 假设您正在寻找局部视图:the syntax 来呈现另一个视图的局部视图控制器是render 'folder/partial'
  • 通过这样做 Jarvis 我现在得到以下错误“未定义的局部变量或方法 `cmets'”
  • 看起来您没有将照片对象传递给视图。我觉得这一定是photos_controllershow 方法中的一个问题 - 你必须做更多的挖掘
  • 我更新了问题以包含 PhotosController 类。除了我在之前的回调中加载图片之外,这几乎是样板,我希望这张照片会被传递。虽然我不确定。你能看出控制器有什么问题吗?另请注意,我确实尝试在 show 方法中调用 @photo = Photo.find(params[:id]) ,但没有效果。
【解决方案2】:

感谢 Marc 和 Jarvis 的所有帮助,我仍然不知道为什么这不起作用,但是查看 api.rubyonrails.org 上的 ActionView::PartialRender 我发现这确实有效...

  - @photo.comments.each do |comment|
    = render partial: 'comments/comment', locals: { comment: comment }

我基本上必须自己进行迭代,尽管在 Marc 引用的指南中明确表示我应该能够做我所写的。

哦,好吧,继续下一个问题。

再次感谢!

【讨论】:

  • = render partial: "comments/comment", collection: @photo.comments 之类的东西应该可以工作
  • 是的,我已经尝试过了,实际上,那是原始代码,然后我什至开始尝试让它工作,但这是不行的。
猜你喜欢
  • 2016-04-20
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-19
  • 2017-07-05
  • 2013-08-20
相关资源
最近更新 更多