Reputation: 8451
Currently I'm writing a feature spec for setting a comment in the database. Everything seems straight forward but I'm getting a nil class error on comments and I can't figure out why? I'll post some code and see if anybody can help me debug this problem.
SPEC:
require "rails_helper"
RSpec.feature "Create a Comment" do
scenario "Customer can leave additional information" do
visit "/comments/new"
user = FactoryGirl.create(:user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
fill_in "comment_fav_drink", with: "Latte"
click_button "Send"
expect(page).to have_current_path('subscribers/search')
expect(page).to have_content("Subscriber Has Been Successfully Created")
end
end
CONTROLLERS:
class CommentsController < ApplicationController
def new
@comment = Comment.new
end
def create
@subscriber = Subscriber.order('updated_at desc').first
@comment = @subscriber.comments.build(comments_params)
if @comment.save
flash[:notice] = "Thank you!"
redirect_to subscribers_search_path(:comments)
else
render "new"
end
end
private
def comments_params
params.require(:comment).permit(:fav_drink, :subscriber_id)
end
end
Upvotes: 0
Views: 265
Reputation: 27961
The only place where you're calling the comments
method is @subscriber.comments
in your create
action, which means that @subscriber
is nil
, looking back up you set that to Subscriber.order('updated_at desc').first
which means that returned nil
which means you have no subscribers in your database when your test is being run, which matches what I can see of your test (ie. you're not creating any subscribers)
Upvotes: 1