Login Engine and current_user

This is likely a painfully newbie-ish question, so please bare with me
:slight_smile:

I’m muddling through my first rails application, and decided to use the
Login Engine to handle user authentication. Now, according to the
README, I should be able to “use current_user method provided by
UserHelper”, like so:

      Welcome <%= current_user.name %>

I’ve put that line in my application.rhtml, and return the following
error:

undefined local variable or method `current_user' for

#<#Class:0xb7875280:0xb7875258>

which seems to suggest I haven’t done something correctly.

Now, I can find where current_user is set in
vendor/plugins/login_engine/lib/login_engine/authenticated_system.rb

I’m still trying to work out what goes where in rails, but I figured in
order for the method to be available globally, I needed to move that
definition into app/controllers/application.rb. Still received the same
error, however.

I then tried adding the definition for current_user to
app/helpers/application_helper.rb, and that sort of did the trick.
“Welcome <%= current_user.name %>” now translates properly in my layout.

My real goal, however, is to get usermonitor to work
(http://wiki.rubyonrails.org/rails/pages/Howto+Add+created_by+and+updated_by/versions/7),
and current_user still does not appear to be accessible by this module.
When I attempt to edit an object in one of my models, I get the error:

undefined method `current_user' for User:Class

So I gather app/helpers/application_helper.rb is likewise not the
correct place for this.

I then try putting it in config/environment.rb (which seems even less
likely, though at this point I do have to concede ‘what do I know?’),
which also appears to work as far as my application.rhtml is concerned.
But when I try to edit an object, I now get:

private method `current_user' called for User:Class

This feels a bit like progress, but I’m stumped regarding how to make
this method public (and whether or not that’s really what I want to do).
If I understand things correctly, methods are public by default, and
only become private when “private” is placed before their definition?
Which is fine, except that there is no instance of “private” in my
config/environment.rb

At this point, I’m stumbling blind, and don’t know if I’m even remotely
headed in the right direction. If somebody could give me a nudge in that
right direction, I would be so very grateful!

Thanks in advance,

Gwen

Did you follow the README, particularly the lines talking about adding
certain include lines to ApplicationHelper?

  • james

James A. wrote:

Did you follow the README, particularly the lines talking about adding
certain include lines to ApplicationHelper?

Thank you for the response!

I installed the Login Engine per the instructions in the README, yes. If
by “certain include lines to ApplicationHelper”, you mean

    include LoginEngine

I did indeed add it. If there are multiple lines to be added, I am
definitely missing them. I just tried grepping the README file, and am
only coming up with that one include.

The problem may actually be with usermonitor, rather than with
login_engine :frowning:

The full trace, if it offers any additional clues, is:

NoMethodError in ClientsController#update

undefined method `current_user’ for User:Class
RAILS_ROOT: …/config/…

Application Trace | Framework Trace | Full Trace
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1129:in
method_missing' #{RAILS_ROOT}/lib/usermonitor.rb:24:inupdate’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1718:in
create_or_update_without_callbacks' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/callbacks.rb:249:increate_or_update’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1392:in
save_without_validation' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:724:insave_without_transactions’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:in
save' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:intransaction’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:91:in
transaction' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:118:intransaction’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:in
save' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1439:inupdate_attributes’
#{RAILS_ROOT}/app/controllers/clients_controller.rb:39:in update' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/base.rb:910:insend’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/base.rb:910:in
perform_action_without_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/filters.rb:368:inperform_action_without_benchmark’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/benchmarking.rb:69:inmeasure’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/rescue.rb:82:inperform_action’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/base.rb:381:in
send' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/base.rb:381:inprocess_without_filters’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/filters.rb:377:in
process_without_session_management_support' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/session_management.rb:117:inprocess’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.4/lib/dispatcher.rb:38:in
`dispatch’
dispatch.cgi:10
EOT

Gwen

I think this is something to do with the user monitor - the login
engine doesn’t add any ‘current_user’ method to the User model at
all…

  • james

I’m muddling through my first rails application, and decided to use the
Login Engine to handle user authentication. Now, according to the
README, I should be able to “use current_user method provided by
UserHelper”, like so:

      Welcome <%= current_user.name %>

I’ve put that line in my application.rhtml, and return the following
error:

Bit of a late reply but I just ran into the exact same problem.

The LoginEngine README is wrong. There is no “name” field for
current_user. Use current_user.login instead and it will work just fine.

Carl

James A. wrote:

I think this is something to do with the user monitor - the login
engine doesn’t add any ‘current_user’ method to the User model at
all…

That explains it then :slight_smile: I thought that login_engine did add a
‘current_user’ method to the User model (I was misunderstanding the bit
from the README which read “You can also use the ‘current_user’ method
provided by UserHelper”). I see my mistake now, though – a helper
method is available to views not models.

So I do still need to add the method to my model. This I can do :slight_smile:

Thanks again for your help!

Gwen

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs