title: Rails: undefined local variable or method 'user'
body: <p>I'm currently on chapter 9 of Hartl's tutorial.  I just added a few tests to check that the 'edit' page works.  The tests should be passing according to the tutorial but 5 of them failed.  I think the mistake might be in my user_pages_spec.rb file (posted below).  According to the command line, there is something wrong with it: 'undefined local variable or method 'user' '.</p>
  
  <pre><code>C:\Sites\sample_app&gt;bundle exec rspec spec/
  ............................................FFFFF
  
  Failures:
  
    1) User pages signup with valid information
       Failure/Error: fill_in "Password",         with: user.password
       NameError:
         undefined local variable or method `user' for #&lt;RSpec::Core::ExampleGroup
  ::Nested_4::Nested_3::Nested_3:0x5259a68&gt;
       # ./spec/requests/user_pages_spec.rb:47:in `block (4 levels) in &lt;top (requi
  red)&gt;'
  
    2) User pages signup with valid information
       Failure/Error: fill_in "Password",         with: user.password
       NameError:
         undefined local variable or method `user' for #&lt;RSpec::Core::ExampleGroup
  ::Nested_4::Nested_3::Nested_3:0x52b0810&gt;
       # ./spec/requests/user_pages_spec.rb:47:in `block (4 levels) in &lt;top (requi
  red)&gt;'
  
    3) User pages signup with valid information
       Failure/Error: fill_in "Password",         with: user.password
       NameError:
         undefined local variable or method `user' for #&lt;RSpec::Core::ExampleGroup
  ::Nested_4::Nested_3::Nested_3:0x5339058&gt;
       # ./spec/requests/user_pages_spec.rb:47:in `block (4 levels) in &lt;top (requi
  red)&gt;'
  
    4) User pages signup with valid information
       Failure/Error: fill_in "Password",         with: user.password
       NameError:
         undefined local variable or method `user' for #&lt;RSpec::Core::ExampleGroup
  ::Nested_4::Nested_3::Nested_3:0x53abec8&gt;
       # ./spec/requests/user_pages_spec.rb:47:in `block (4 levels) in &lt;top (requi
  red)&gt;'
  
    5) User pages signup with valid information
       Failure/Error: fill_in "Password",         with: user.password
       NameError:
         undefined local variable or method `user' for #&lt;RSpec::Core::ExampleGroup
  ::Nested_4::Nested_3::Nested_3:0x5412630&gt;
       # ./spec/requests/user_pages_spec.rb:47:in `block (4 levels) in &lt;top (requi
  red)&gt;'
  
  Finished in 1.03 seconds
  49 examples, 5 failures
  
  Failed examples:
  
  rspec ./spec/requests/user_pages_spec.rb:52 # User pages signup with valid infor
  mation
  rspec ./spec/requests/user_pages_spec.rb:53 # User pages signup with valid infor
  mation
  rspec ./spec/requests/user_pages_spec.rb:55 # User pages signup with valid infor
  mation
  rspec ./spec/requests/user_pages_spec.rb:54 # User pages signup with valid infor
  mation
  rspec ./spec/requests/user_pages_spec.rb:56 # User pages signup with valid infor
  mation
  </code></pre>
  
  <p>user_pages_spec.rb</p>
  
  <pre><code>require 'spec_helper'
  
  describe "User pages" do
  
    subject { page }
  
    describe "profile page" do
      let(:user) { FactoryGirl.create(:user) }
      before { visit user_path(user) }
  
      it { should have_content(user.name) }
      it { should have_title(user.name) }
    end
  
    describe "signup page" do
      before { visit signup_path }
  
      it { should have_content('Sign up') }
      it { should have_title(full_title('Sign up')) }
    end
  
    describe "signup" do
  
      before { visit signup_path }
  
      let(:submit) { "Create my account" }
  
      describe "with invalid information" do
        it "should not create a user" do
          expect { click_button submit }.not_to change(User, :count)
        end
      end
  
    describe "edit" do
      let(:user) { FactoryGirl.create(:user) }
      before do
        sign_in user
        visit edit_user_path(user)
      end
    end
      describe "with valid information" do
        let(:new_name)  { "New Name" }
        let(:new_email) { "new@example.com" }
        before do
          fill_in "Name",             with: new_name
          fill_in "Email",            with: new_email
          fill_in "Password",         with: user.password
          fill_in "Confirm Password", with: user.password
          click_button "Save changes"
        end
  
        it { should have_title(new_name) }
        it { should have_selector('div.alert.alert-success') }
        it { should have_link('Sign out', href: signout_path) }
        specify { expect(user.reload.name).to  eq new_name }
        specify { expect(user.reload.email).to eq new_email }
      end
    end
  end
  </code></pre>
  
  <p>users.controller.rb</p>
  
  <pre><code>class UsersController &lt; ApplicationController
  
    def show
      @user = User.find(params[:id])
    end     
  
    def new
      @user = User.new
    end
  
    def create
      @user = User.new(user_params)
      if @user.save
        sign_in @user
        flash[:success] = "Welcome to the Sample App!"
        redirect_to @user
      else
        render 'new'
      end
    end
  
    def edit
      @user = User.find(params[:id])
    end
  
    def update
      @user = User.find(params[:id])
      if @user.update_attributes(user_params)
        flash[:success] = "Profile updated"
        redirect_to @user
      else
        render 'edit'
      end
    end
  
  
    private
  
      def user_params
        params.require(:user).permit(:name, :email, :password,
                                     :password_confirmation)
      end
  end
  </code></pre>