Problem with undefined variable current_user

71 views
Skip to first unread message

Jen

unread,
Jun 12, 2011, 3:43:01 PM6/12/11
to rubyonra...@googlegroups.com
Hi everyone,
I'm working through the tutorial at the following link and attempting to customise/adapt the lessons for my own slightly different application.

Link:
Ruby on Rails Tutorial: Learn Rails by Example | Ruby on Rails 3 Tutorial book and screencasts | by Michael Hartl

I'm currently trying to work through chapter 10, but having some problems with the variable 'current_user' not being properly defined in the 'signed_in' method. Everything looks fine to me, but  as I am obviously missing something I'd appreciate it if someone with more experience at debugging could point me in the right direction.

I can sign in with my test users and the exception only occures when attempting to edit a profile.

Controller and model are attached.
Cheers,
Jen.

Error:

NameError in UsersController#edit

undefined local variable or method `current_user' for #<UsersController:0x9ffad80>

Rails.root: /home/resource_portal/website

Application Trace | Framework Trace | Full Trace
app/helpers/sessions_helper.rb:17:in `signed_in?'
app/controllers/users_controller.rb:91:in `authenticate'
rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:436:in `_run__1056153724__process_action__372525482__callbacks'
rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:93:in `run_callbacks'
rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/abstract_controller/callbacks.rb:17:in `process_action'
rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/notifications.rb:52:in `block in instrument'
rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/notifications.rb:52:in `instrument'
rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/action_controller/metal/rescue.rb:17:in `process_action'
...

users_controller.rb
sessions_helper.rb

Tim Shaffer

unread,
Jun 13, 2011, 9:41:48 AM6/13/11
to rubyonra...@googlegroups.com
The error message is pretty clear - You haven't defined a current_user method in your helper.

Check out Listing 9.16 in that tutorial you posted. That's where it describes the current_user method you need to add.

http://ruby.railstutorial.org/chapters/sign-in-sign-out#code:current_user_working

Simon Baumgartner

unread,
Jun 13, 2011, 9:49:28 AM6/13/11
to Ruby on Rails: Talk
Are you including SessionsHelper in you ApplicationController ?

class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end

Simon

On Jun 12, 3:43 pm, Jen <jen.bot...@gmail.com> wrote:
> Hi everyone,
> I'm working through the tutorial at the following link and attempting to
> customise/adapt the lessons for my own slightly different application.
>
> Link:
> Ruby on Rails Tutorial: Learn Rails by Example | Ruby on Rails 3
> Tutorial book and screencasts | by Michael Hartl
> <http://ruby.railstutorial.org/ruby-on-rails-tutorial-book>
>
> I'm currently trying to work through chapter 10, but having some
> problems with the variable 'current_user' not being properly defined in
> the 'signed_in' method. Everything looks fine to me, but  as I am
> obviously missing something I'd appreciate it if someone with more
> experience at debugging could point me in the right direction.
>
> I can sign in with my test users and the exception only occures when
> attempting to edit a profile.
>
> Controller and model are attached.
> Cheers,
> Jen.
>
> Error:
>
>   NameError in UsersController#edit
>
> undefined local variable or method `current_user' for #<UsersController:0x9ffad80>
>
> |Rails.root: /home/resource_portal/website|
>
> Application Trace <http://localhost:3000/users/1/edit#> | Framework
> Trace <http://localhost:3000/users/1/edit#> | Full Trace
> <http://localhost:3000/users/1/edit#>
>
> |app/helpers/sessions_helper.rb:17:in `signed_in?'
> app/controllers/users_controller.rb:91:in `authenticate'
> rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/callbacks .rb:436:in `_run__1056153724__process_action__372525482__callbacks'
> rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/callbacks .rb:409:in `_run_process_action_callbacks'
> rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/callbacks .rb:93:in `run_callbacks'
> rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/abstract_controller/callbac ks.rb:17:in `process_action'
> rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/action_controller/metal/ins trumentation.rb:30:in `block in process_action'
> rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/notificat ions.rb:52:in `block in instrument'
> rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/notificat ions/instrumenter.rb:21:in `instrument'
> rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/notificat ions.rb:52:in `instrument'
> rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/action_controller/metal/ins trumentation.rb:29:in `process_action'
> rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/action_controller/metal/res cue.rb:17:in `process_action'|
> ...
>
>  users_controller.rb
> 2KViewDownload
>
>  sessions_helper.rb
> 1KViewDownload

Jen

unread,
Jun 13, 2011, 10:19:15 AM6/13/11
to rubyonra...@googlegroups.com
Hi,
Moved the authenticate method to the helper, after looking at the sample
app code from github. This seems to have solved the problem.

Thanks,
Jen.

Reply all
Reply to author
Forward
0 new messages