4

I am trying to get the error flash to appear in the railstutorial.org sample app. So far this is what I have done. Can anyone help me find the error:

My RSPEC test fail:

1) SessionsController POST 'create' invalid signin should have a flash.now message
Failure/Error: flash.now[:error].should =~ /invalid/i
expected: /invalid/i
     got: nil (using =~)
./spec/controllers/sessions_controller_spec.rb:27

Controller code:

def create
  user = User.authenticate(params[:session][:email],
  params[:session][:password])
  if user.nil?
    flash.now[:error] = "Invalid email/password combination."
    @title = "Sign in"
    render 'new'
  else
    # Sign the user in and redirect to the user's show page.
  end
end

Test code:

describe "POST 'create'" do

describe "invalid signin" do

  before(:each) do
    @attr = { :email => "example@gmail.com", :password => "invalid"}
  end 

  it "should re-render the new page" do
    post :create, :session => @attr
    response.should render_template('new')
  end 
  it "should have a flash.now message" do
    post :create, :session => @attr
    flash.now[:error].should =~ /invalid/i
  end 
end 
jsttn
  • 1,495
  • 3
  • 16
  • 21
  • Yes, again, "Tried posting this before and my account got messed up" and now I can't edit my original post to include more code. – jsttn May 10 '11 at 00:22
  • Isee the problem..an edit was waiting for approval. I have approved your edit request in the other question. You should delete one of the questions. Also, I have asked a moderator to un-close this question and close the other one (but you should probably delete it yourself) – Zabba May 10 '11 at 00:35
  • Yep, thanks so much. I'm learning my way around! – jsttn May 10 '11 at 00:57
  • What is the result with `flash.now[:error].should match /Invalid/` ? And just for testing the test's sake, do a `puts flash.now[:error]` -- is that printed as nil? And another thought.. what if you print the entire flash: `puts flash.inspect` -- do you see the "invalid" message in there? – Zabba May 10 '11 at 07:13
  • Merged your accounts, and deleted the other question. This is no longer a dupe, folks. –  May 10 '11 at 14:01
  • it looks like your session is fine since the first test passes. Perhaps then the flash code is incorrect. Can you check again the code listing in section 8.16 to make sure the flash is working? http://ruby.railstutorial.org/chapters/sign-up#code:layout_flash Do you see the error message when you fail to sign in using a browser? – GrahamJRoy May 10 '11 at 22:45
  • No, because when the user signs up, it works. – jsttn May 11 '11 at 01:33
  • can you post your git hub repo? I can't see why it wouldn't be working – GrahamJRoy May 11 '11 at 07:23
  • Prefer not to, here's the file though http://cl.ly/3i2Z211h1X1T0s1Y3K19 – jsttn May 11 '11 at 21:40

1 Answers1

2

It looks like in your app/controllers/sessions_controller.rb you have a second create method declared at the bottom that just says:

def create
  render 'new'
end

That second declaration is superseding your intended create method earlier in the file. Just delete those three lines (20-22 in your file) and your specs pass with flying colors, whereby I mean just one color, namely green.

T.J. Schuck
  • 3,645
  • 2
  • 19
  • 20
  • Been there with the [redefinition of a method in a controller](http://stackoverflow.com/questions/372652/what-are-the-ruby-gotchas-a-newbie-should-be-warned-about/5508658#5508658). grr. – Zabba May 12 '11 at 06:50
  • Awesome! Thanks for your help and thanks Zabba for clearing up the post issue. – jsttn May 12 '11 at 19:25
  • And don't let this discourage you -- these things happens to even [the best of us](http://ryanbigg.com/2011/03/code-doesn-t-protect-against-stupid). Keep it up with the RailsTutorial.org book -- it's great, and actually how I learned Rails myself! – T.J. Schuck May 14 '11 at 05:05