【发布时间】:2016-08-10 06:41:55
【问题描述】:
我正在使用 rspec 测试 create 操作,它给了我:
ActiveRecord::RecordNotFound: 找不到带有 'id'=:post_id 的帖子
不确定,为什么会这样,有什么建议吗?
规格:
describe CommentsController do
it "#create" do
post1 = create(:post)
post :create, comment: attributes_for(:comment, post_id: post1.id)
p Comment.count
end
end
创建动作:
def create
@comment = @post.comments.build(comment_params)
@comment.user_id = current_user.id
respond_to do |format|
if @comment.save
format.json { render json: @comment }
end
end
end
行动前:
before_action :authenticate_user!
before_action :find_post, only: [:index, :create, :destroy]
路线:
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
post_comment DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
【问题讨论】:
-
请分享一些堆栈跟踪和您的
find_post方法。
标签: ruby-on-rails ruby ruby-on-rails-4 rspec-rails