Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Simple Ruby on Rails Application Testing the Whole App Before Filters

Alexander Lee
Alexander Lee
1,420 Points

login_path vs. new_user_session_path

The video doesn't explain why we cant use login_path vs. new_user_session_path very well. I looked at rake routes and they have the same Controller Action.. what gives?

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, when you run rake routes, look in the middle column. This tells you what URI pattern is used for both paths and they are different. One is /login(.:format) and the other one is /users/sign_in(.:format). If your controller is set to redirect to one of them, the test will only succeed if it checks the very same path/URI. At some point your before_action :authenticate_user! in Devise was set to redirect to one of those particular paths, not the other. I think new_user_session_path is Devise's default. I'm sure there are ways to change that behavior, you just have to dig in the documentation ;).

Alexander Lee
Alexander Lee
1,420 Points

I understood the rake routes differences but I couldn't understand why the redirect was one path over the other. Thank you...that cleared up a lot!

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

These are the same paths, the login_path is just customized to look better - login_path sounds much better than new_user_session_path when you just look at it and want to infer its function :). There's a lot of stuff you can do with paths in Rails.

Alexander Lee
Alexander Lee
1,420 Points

Thanks for the fast response!

I guess what I'm asking is why the test fail if I tried to use login_path vs. new_user_session_path.

I know we created the login path through our config routes. What part of the code is looking specifically for the new_user_session_path in our test?

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Can you show me your code? On github preferably. I can't blindly assess your particular case.

Alexander Lee
Alexander Lee
1,420 Points

I will provide a link below.. I am still new to github so I'm not sure if this is the best way to access it but

https://github.com/kkomaz/treebook/commit/aca07b5385a725645b25e37e57740ad9bdd3bfd1

test/controllers/statuses_controller_test.rb

  • test "should get new" do
  • test "should be redirected when not logged in" do
  • get :new
  • assert_response :redirect
  • assert_redirected_to new_user_session_path
  • end