current_user not working but user_signed_in is true

1,019 views
Skip to first unread message

Jeremy

unread,
Aug 14, 2012, 1:15:41 AM8/14/12
to plataforma...@googlegroups.com
Hello,

I am a brand new Rails developer.  I am attempting to use Devise for authentication, but I'm running into an issue.

The default login seems to work.  I can create a new user and then login with those credentials (email/password).

I have a very simple controller for the home page that I'm using to test...

class HomeController < ApplicationController
  before_filter :authenticate_user!
  def index
    @user = current_user
  end
end

Then in my home view I'm trying to display the user information...

<% if user_signed_in? -%>
<p>signed in as <% current_user.id -%></p>
<p>user = <% @user.id -%></p>
<% end -%>

The if statement evaluates as true, but I just see...

signed in as

user =

In other words, the current_user doesn't work either in the view or in the controller, though I have put 'before_filter :authenticate_user!' into my controller.

I am a bit mystified at this point.

Here are my software versions...

ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]

rails 3.2.7
devise 2.1.2

I may be missing something very simple here, but my understanding is that 'current_user' should be set if 'user_signed_in' is true.

Thanks for any help you can give on this.

--Jeremy

thiagocifani

unread,
Aug 14, 2012, 8:02:06 AM8/14/12
to plataforma...@googlegroups.com

On 14/08/2012, at 02:15, Jeremy <jermcc...@gmail.com> wrote:

Hello,

I am a brand new Rails developer.  I am attempting to use Devise for authentication, but I'm running into an issue.

The default login seems to work.  I can create a new user and then login with those credentials (email/password).

I have a very simple controller for the home page that I'm using to test...

class HomeController < ApplicationController
  before_filter :authenticate_user!
  def index
    @user = current_user
  end
end

Then in my home view I'm trying to display the user information...

<% if user_signed_in? -%>
<p>signed in as <% current_user.id -%></p>
<p>user = <% @user.id -%></p>
<% end -%>

The if statement evaluates as true, but I just see...

signed in as

user =

In other words, the current_user doesn't work either in the view or in the controller, though I have put 'before_filter :authenticate_user!' into my controller.

Jeremy, first of all, why you need to show user's id instead of user email or something more reliable! To make sure that any content is going to show in your views you can't forget the equal symbol after the percentage. You should always do something like the snippet above, when you wish to show a output:

<%= current_user.email %>

I hope you get it! I also highly recommend you to read rails tutorial book on railstutorial.org to clarify your mind with some basics.

I am a bit mystified at this point.

Here are my software versions...

ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]

rails 3.2.7
devise 2.1.2

I may be missing something very simple here, but my understanding is that 'current_user' should be set if 'user_signed_in' is true.

Thanks for any help you can give on this.

--Jeremy

--
 
 
 

Jeremy

unread,
Aug 14, 2012, 6:43:37 PM8/14/12
to plataforma...@googlegroups.com
Hello,

Thanks for the replies! 

Indeed this was 100% operator error on my part.  I just ommitted the "=" to write to the page.  

--Jeremy
Reply all
Reply to author
Forward
0 new messages