Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby User Authentication with Rails Password Hashing and Sign In Integration testing authentication

flash[:success] test is not passing unless I fake it out.

When I literally copy the code from the project file for the users_controller, I get this failed test:

"1) UsersController POST create with valid params sets the flash success message Failure/Error: expect(flash[:success]).to eq("Thanks for signing up!")

expected: "Thanks for signing up!"
got: nil

(compared using ==)"

Here's what I have in the users_controller under the create function:

def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        session[:user_id] = @user.id
        flash[:success] = "Thanks for signing up!"
        format.html { redirect_to todo_lists_path, flash[:success]=>'Thanks for signing up!' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
   end

When I add this code above the format.html:

flash[:success] = "Thanks for signing up!"

then the test passes.

I'm pretty sure I'm passing the test, though, without getting the right code. Any ideas why the test doesn't pass with the project code as is?

Thanks

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

You published it correctly on github :). You will get more comfortable with it in no time. Here's your solution. Add this line to your application_controller.rb:

add_flash_types :success

Jason did this at some point since he has this line in his code under the video. Without that line your Rails app only knows some default flash types like 'notice'. In this line you can create your own types and then refer to them in your controllers.

Hi Maciej - thank you so much! That worked like a charm, and even better, I understand now why it wasn't working because of your comment.

Gabriele Rossi
Gabriele Rossi
5,887 Points

Awesome! Worked like a charm, thank you Maciej!

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

There are two ways to get what you want. First one is, like you did, adding the separate line for setting flash message, the other one looks like this:

redirect_to todo_lists_path, success: 'Thanks for signing up!'

It's shorter and requires a slightly different syntax compared to what you did. And that is what Jason has in his controller around 1:14 of the video.

Hi Maciej,

I should have clarified in my comment - when I try Jason's strategy, I get the failed test that I posted above. Any thoughts on why that would be? To check again, I literally copied and pasted your code into that users controller.

Thanks, Syam

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

Please put your code on GitHub and I will deploy locally to check it out.

Hi Maciej,

Here's my code on Github, I'd really appreciate if you're able to explain this test that doesn't pass: https://github.com/spalak/odot-flash

Also, I have to admit this is the first time I've committed code to GitHub (as opposed to Git on my computer) so my apologies if the link above does not work.

Thanks,