0

Next newbie problem in Hartl's Tutorial. Now in chapter 7. . While I'm running App I'm getting something like this in my browser:

ActiveRecord::RecordNotFound in UsersController#show

Couldn't find User with id=1

Rails.root: C:/rails_project/my_app
Application Trace | Framework Trace | Full Trace

app/controllers/users_controller.rb:4:in `show'

Request

Parameters:

{"id"=>"1"}

Show session dump

Show env dump
Response

Headers:

None

My user_controller.rb looks like this:

   class UsersController < ApplicationController

     def show
    @user = User.find(params[:id])
    describe "profile page" do  
    #Code to make a user variable
    before { visit user_path(user) }

        it { should have_selector('h1',    :text => user.name) }
        it { should have_selector('title', :text => user.name) }
    end
  end


  def new
  end


  end

Thanks for help & intrest!

szatan
  • 517
  • 4
  • 8
  • 26
  • 1
    Are you sure you have users in your DB? – Stefan H Aug 09 '12 at 21:13
  • Call me lame, but how can I check this? – szatan Aug 09 '12 at 21:15
  • Try this: http://sqlitebrowser.sourceforge.net/ and take a look at what is in your sqlite DB – Stefan H Aug 09 '12 at 21:16
  • 3
    Maybe I'm missing something, but did you put your test code in with your controller code? I've always understood that the test code goes in a separate file. – Tyler DeWitt Aug 09 '12 at 21:32
  • You're right, but that didn't solve the problem. – szatan Aug 09 '12 at 21:47
  • 1
    Have you checked whether you have users or not yet? An alternative way to do this could be to go into the command line and type `rails c --sandbox` followed by `User.find(1)` – TangoKilo Aug 09 '12 at 23:06
  • Ok. I've checked this. And something strange appeared. It did't found any user and when I'am trying to create new with "user = User.new(name:"blabla" , email: "blabla@bla.com" )" I'am getting user with no user id ( user Id = nil). When I'am trying to save it I'am getting false". Any ideas? – szatan Aug 10 '12 at 17:18
  • Ok, I've found the solution. The key was to create new db (probably I've reseted db, not easy to be newbie!). Thx for help! – szatan Aug 10 '12 at 20:53

1 Answers1

0

You can use find_by_id instead of find.

find results in an error when the parameter doesn't exists in the model.

See also: difference between find, where and find_by_id

Community
  • 1
  • 1
libitte
  • 1
  • 3