Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if user is signed in with rails devise?

I am trying to add the header to my rails application based on authenticating the user. So here I am checking that if the user has logged in or signed in and then adding login/logout link based on that.

But I am getting the following error:

application.html.erb:16: syntax error, unexpected tSYMBEG, expecting keyword_do or '{' or '(' ...roy_user_session_path, method :delete );@output_buffer.safe_

Here's what I have tried:

<% if user_signed_in? do %> 
    <%= link_to "Log out", destroy_user_session_path, method :delete %>
<% else %>
    <%= link_to "login", new_user_session_path %>
<% end %>

How can I resolve this?

like image 571
CJAY Avatar asked Jan 13 '17 07:01

CJAY


People also ask

How do you check if a user is signed in rails?

If you want to check whether user is signed for every action in the application, you have to put the filter in the application controller. You can do this for a specific controller also.

What does devise do in Rails?

Devise is the cornerstone gem for Ruby on Rails authentication. With Devise, creating a User that can log in and out of your application is so simple because Devise takes care of all the controllers necessary for user creation ( users_controller ) and for user sessions ( users_sessions_controller ).

Where is devise session stored?

By default, session data in Rails is stored via a cookie in the user's browser. It's a nice, simple storage mechanism, but it means that the server has absolutely no “memory” of a given session. This can cause security problems for your application.


2 Answers

First of all remove do from this line, you don't need that

<% if user_signed_in? %>

Secondly add : after method, it's a key value pair

<%= link_to "Log out", destroy_user_session_path, method: :delete %>

Hope that helps!

like image 170
Rajdeep Singh Avatar answered Oct 07 '22 10:10

Rajdeep Singh


You are making a syntax error in method delete. Copy the below code

<% if user_signed_in? %>
    <%= link_to "Log out", destroy_user_session_path, :method=>'delete'%>
<% else %>
    <%= link_to "login", new_user_session_path %>
<% end %>
like image 5
Aniket Tiwari Avatar answered Oct 07 '22 09:10

Aniket Tiwari