尝试显示照片评论,照片属于个人资料。 html不呈现。
相关代码:
的routes.rb
resources :profiles do
resources :photos do
resources :comments do
resources :comments
end
end
end
评论/ _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}
模型/ photo.rb
class Photo < ActiveRecord::Base
belongs_to :profile
has_many :comments, as: :commentable, :dependent => :destroy
end
模型/ comment.rb
class Comment < ActiveRecord::Base
belongs_to :profile
belongs_to :commentable, polymorphic: true
has_many :comments, as: :commentable, :dependent => :destroy
end
模型/ profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
has_many :photos, :dependent => :destroy
has_many :comments, :dependent => :destroy
end
应用程序/控制器/ 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
我似乎无法破解这个,我知道这不是魔术,但我显然没有看到什么。
感谢您的帮助!
答案 0 :(得分:0)
将show.html.haml
更改为:
%ul
- render 'comments/comment', locals: {comments: @photo.comments}
原因是,您无法在视图中呈现视图,因此上述内容将假设您正在评论文件夹中查找名为_comment.html.haml
的部分视图。
答案 1 :(得分:0)
感谢Marc和Jarvis的所有帮助,我仍然不知道为什么这不起作用,但是在api.rubyonrails.org上查看ActionView :: PartialRender我发现这确实有效。 ..
- @photo.comments.each do |comment|
= render partial: 'comments/comment', locals: { comment: comment }
我基本上必须自己进行迭代,尽管在Marc引用中明确指出它应该能够做我写过的内容。
哦,好吧,接下来的问题。
再次感谢!