【发布时间】: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