【发布时间】:2015-08-04 18:48:15
【问题描述】:
好的,伙计们,作为我在 Bloc 的项目之一,我正在创建一个 reddit 克隆。对于这个特定的任务,我将创建一个功能,用户可以在其中评论每个帖子(witch 已经嵌套在每个主题中)。但是,当我尝试查看与某个主题相关联的帖子时遇到此错误。
ArgumentError in Posts#show
'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
到目前为止,我已经创建了 cmets 控制器,在 cmets 和用户之间建立了外键连接,使 cmets 路由嵌套在帖子路由中,创建了一个用于提交评论的表单部分和一个在 post/show 中的评论部分,并创建了一个 CommentPolicy 以授权用户创建新的 cmets。
我检查了我的数据库,并且 cmets 确实存在,因为我将它们添加到我的种子文件中。我怀疑我的错误在于我的表格和我对部分内容的引用,但我有点难过。在这里的任何帮助将不胜感激。谢谢!
我的代码:
用户.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
cmets_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
cmets/_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 %>
cmets/_comment.html.rb
<%= comment.body %>
posts/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
种子文件中的cmets
#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
【问题讨论】:
标签: ruby-on-rails forms partials