vanutsteen.nl => nerds only A blog on rails, php, computing, my bass guitar and stuff

Ruby on rails: acts_as_authenticated and current_user in a model

on in Development, Ruby

I’m doing lots of Ruby on Rails development lately. I installed actsasauthenticated to create a login for my newest project. But I also wanted to add automagically updatedby and createdby fields for my models.

I used usermonitor.rb for this. But usermonitor expects you to have: User.current_user. But ‘helaas’, this method doesn’t exist. So I had to add it. This is what I did:

1
2
3
4
5
6
7
8
9
10
11
12
13
 class ApplicationController < ActionController::Base

  include AuthenticatedSystem
  before_filter :login_from_cookie # <-- if you use this
  before_filter :set_current_user

  # rest of your ApplicationController body

  protected

  def set_current_user
    User.current_user = self.current_user
  end

And in my User-model I added:

1
cattr_accessor :current_user

Worked like a charm!

Comments