Saran
Saran

Reputation: 713

Current_user devise method returns nil outside the user controller

Is this a scope/routes problem? The current_user works in the user controller. Outside, in another controller/view, this returns nil. Even if the user is logged in, the user_signed_in? method returns false. I know I am missing something, just not able to point my finger. What is the right way to do this?

Upvotes: 2

Views: 9055

Answers (4)

orourkedd
orourkedd

Reputation: 6421

Same thing happened to me but the problem was when I made an ajax request. This fixed it (in haml). This makes sure that all ajax request carry the csrf token:

:coffee
  require ['jquery'], ($)->
    $.ajaxSetup
      headers:
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')

Make sure this is in the header:

= csrf_meta_tags

Upvotes: 0

sites
sites

Reputation: 21815

I was having the same problem, I needed to put this in my layout, I was using a different layout than application:

= csrf_meta_tags

Upvotes: 2

Saran
Saran

Reputation: 713

Found the problem. It was the sessions. Apparently, the current_user and other methods were not behaving right because of cookies. I had subdomains and the problem was because of that https://github.com/fortuity/rails3-subdomain-devise/wiki/Tutorial-%28Walkthrough%29. This git tutorial helped.

Thanks!

Upvotes: 4

Ernst
Ernst

Reputation: 255

It works for me in other controllers. A wild guess: maybe the other controller needs: before_filter :authenticate_user!

Upvotes: 6

Related Questions