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

Ruby on Rails project question

I'm getting through testing the whole app and things are going well.

Just out of interest, the statuses_controller has now got some before_filter rules based on the user being logged in.

2 questions:

What does the "!" at the end of this mean?

before_filter :authenticate_user!

When a user is not logged in and tries to post a status, the test shows that it redirects to the new_user_session_path. How is this set up? Where would you change it/add if you wanted it to redirect to another path?

Thanks

2 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Maxine,

The "!" at the end of "authenticate_user!" is just a naming convention in Ruby. The exclamation point usually denotes that a method is "dangerous" in one way or another. For example, calling the "reverse!" method on a string will modify the original variable while the "reverse" method will return a new string with the characters reversed.

In order to change the redirect to the new_user_session_path when not signed, I think you'd have to write your own custom method instead of "authenticate_user!" in order to do so. You could also rewrite "authenticate_user!" in your controllers. Devise does let you customize the redirected route after a user is signed in, but not before (check out https://github.com/plataformatec/devise for more info -- search for "after_sign_in_path"). You could just write another method in your controller:

def authenticate_user!
  if user_signed_in?
    return true
  else
    flash[:warn] = "You must be signed in to access that page."
    redirect_to root_path
  end
end

Thanks Jason!