I have the following test written in RSpec:
describe '#create' do
...
it 'redirects users to profile page' do
response.should redirect_to user_path(user)
end
...
...
And the following in my UsersController:
def create
@user = User.new(params[:user])
if @user.save
redirect_to user_path(@user)
end
end
Does anyone know why this is returning the following error:
NameError: undefined local variable or method 'user'
I also tried changing this to be root_url in both cases instead of user_path(user) and it gave a different error saying:
Expected response to be a <:redirect>, but was <200>
Does anyone know what the issue might be? I have double-checked my code and have seen similar questions posted online, but haven’t been able to find a solution. Thanks in advance for any help!
It turns out that
assignsjust creates an instance variable and doesn’t populate it without having anotherpost :createabove it.So, for future reference, here’s the correct code:
However, you must use the
assigns :useras Mori suggested in order to create the instance variable in the second part. E.G. You can’t simple douser_path(:user)oruser_path(@user).Hope this helps someone in the future!