好吧,作为我在Bloc的一个项目,我正在创建一个reddit克隆。对于这个特定的任务,我将创建一个功能,用户可以在每个帖子上发表评论(已经嵌套在每个主题中)。但是,当我尝试查看与主题相关联的帖子时,我遇到此错误。
ArgumentError in Posts#show
'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
到目前为止,我已经创建了注释控制器,在注释和用户之间建立了外键连接,使得注释路由嵌套在帖子路由中,创建了一个部分用于评论提交,一个注释部分在post / show中,并创建了一个CommentPolicy,以授权用户创建新的评论。
我检查了我的数据库,并且注释确实存在,因为我将它们添加到我的种子文件中。我怀疑我的错误在于我的形式和我对我的部分的引用,但我有点难过。这里的任何帮助将不胜感激。谢谢!
我的代码:
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :posts
has_many :comments
mount_uploader :avatar, AvatarUploader
def admin?
role == 'admin'
end
def moderator?
role == 'moderator'
end
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
routes.rb
中的相应路线 resources :topics do
resources :posts, except: [:index] do
resources :comments, only: [:create]
end
end
comments_controller:
class CommentsController < ApplicationController
def index
end
def create
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:post_id])
@comments = @post.comments
@comment = current_user.comments.build(params[:comment])
@comment.post = @post
@new_comment = Comment.new
authorize @comment
if @comment.save
flash[:notice] = "Comment was saved"
else
flash[:error] = "There was an error saving this comment. Please try again."
end
end
end
posts_controller:
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
@topic = Topic.find(params[:topic_id])
@comment = Comment.find(params[:id])
authorize @comment
end
def new
@topic = Topic.find(params[:topic_id])
@post = Post.new
authorize @post
end
def create
@topic = Topic.find(params[:topic_id])
@post = Post.new(post_params)
@post.user = current_user
@post.topic = @topic
authorize @post
if @post.save
flash[:notice] = "Post was saved."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving this post. Please try again."
render :new
end
end
def edit
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
authorize @post
end
def update
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
authorize @post
if @post.update_attributes(post_params)
flash[:notice] = "Post was updated."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :edit
end
end
private
def post_params
params.require(:post).permit(:title, :body, :image)
end
end
评论/ _form.html.erb
<%= form for [topic, post, comment] do |f| %>
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter comment here" %>
</div>
<div class="form-group">
<%= f.submit "Add comment", class: "btn btn-success" %>
</div>
<% end %>
评论/ _comment.html.rb
<%= comment.body %>
文章/ show.html.erb
<h1><%= @post.markdown_title %></h1>
<div class="row">
<div class="col-md-8">
<small>
<%= image_tag(@post.user.avatar.tiny.url) if @post.user.avatar? %>
submitted <%= time_ago_in_words(@post.created_at) %> ago by
<%= @post.user.name %>
</small>
<p><%= @post.markdown_body %></p>
<small>
<%= image_tag @post.image_url%>
</small>
</div>
<div class="col-md-4">
<% if policy(@post).edit? %>
<%= link_to "Edit", edit_topic_post_path(@topic, @post), class: 'btn btn-success' %>
<% end %>
</div>
<%= render @comments %>
<% if policy(@comment).create? %>
<h4>New Comment</h4>
<%= render partial: 'comments/form', locals: { topic: @topic, post: @post, comment: @comment } %>
<% end %>
comment_policy
class CommentPolicy < ApplicationPolicy
def new
user.present?
end
def create
user.present?
end
end
种子文件中的评论
#Create Comments
100.times do
Comment.create!(
user: users.sample, # we have not yet associated Users with Comments
post: posts.sample,
body: Faker:: Lorem.paragraph
)
end
comments = Comment.all
答案 0 :(得分:0)
相信错误就在这一行
<%= render @comments %>
在posts/show.html.erb
您@comments
show
方法中未定义posts_controller
。尝试将@comments = @post.comments
放入show
方法。
def show
@post = Post.find(params[:id])
@topic = Topic.find(params[:topic_id])
@comment = Comment.find(params[:id])
authorize @comment
@comments = @post.comments
end