- Read Tutorial
- Watch Guide Video
In this lesson let's learn a little bit more about some advanced methods in devise
that can come handy while developing future applications.
Go to the partial _nav.html.erb
, and there you can see the different links for pages. Here, I want to make use of a devise
method called current_user
. We are going to use this method to help a signed in user to sign out at any time. To do this, add this code:
<% if current_user %> <%= link_to "Sign Out", destroy_user_session_path, method :delete %> <% else %> <%= link_to "Register", new_user_registration_path %> <%= link_to "Sign Out", new_user_session_path %> <% end %>
In this code, I'm checking if any user is logged in, and if so, I'm calling a method called destroy_user_Session_path
that takes a method called delete as its parameter. This method will end the user's session, so that he or she can log out. If no user is logged in, then I have two links - one to register and the other to sign in.
Now, let's check if this is going to work. Refresh your browser and you can see the Sign Out link because you are already logged in.
Now, if you click on Sign Out, you can see two links - Register
and Sign In
because you are not logged in. It even has a message that says Signed out successfully.
So, this is how you can integrate some of the built-in methods like current_user
into your application.