Set instance variable for all actions in the Controller


#1

Can I set an application wide instance variable that is available for
all
actions…and their views?

eg.
class ApplicationController < ActionController::Base
@current_user = User.find(session[:user_id])
end

and everywhere I can call @current_user.id and I can get that object?
Even
down in the views?

I could not get this to work…

Thanks in advance,

Jeff


#2

Hi –

On Thu, 1 Jun 2006, Jeff W. wrote:

I could not get this to work…

What you’ve got there is an instance variable, but it’s an instance
variable that belongs to the class object, ApplicationController,
itself. Like so many things in Ruby, it comes down to: classes are
objects too :slight_smile:

What you want are instance variables that belong to the particular
instance of the controller that gets created and runs the action. You
can do this using a before_filter hook:

class ApplicationController < ActionController::Base
before_filter :set_user
def set_user
@current_user = User.find(session[:user_id])
end
end

etc. You’d probably want to limit it to certain actions, but you can
do that too. (See API docs for before_filter and other related
methods.)

Notice that @current_user is inside an instance method definition. So
when that code gets executed, it will be in the context of a
particular instance of ApplicationController, and that instance will
own the instance variable.

David


David A. Black (removed_email_address@domain.invalid)


#3

Wow, an “a ha” moment for me. This helps out tremendously.

It shows in your response that you are an educator. I have your book and
it
also has helped me fudge my way through learning Ruby.

Thank-you for writing one of the first Rails books when we are so short
on
good, professional documentation.


#4

Great, this works too and I’ll use it. I like that it doesn’t get set
until
I call for the variable. The only thing is I can’t restrict it to
certain
actions but for my purposes this doesn’t matter because I’m only calling
it
from actions where I need it so that’s good enough.

Thanks,

Jeff


#5

class ApplicationController < ActionController::Base
before_filter :set_user
def set_user
@current_user = User.find(session[:user_id])
end
end

You could also do this:

class ApplicationController < ActionController::Base

protected

 def current_user
   @current_user ||= User.find(session[:user_id])
 end
 helper_method :current_user

end

Which means that you don’t look for the current user until you
actually need to, that the implementation of getting the current user
is hidden a little better, and lets you type one less character to
type when getting the current user.


Michael D.
http://www.mdaines.com


#6

before_filter :set_myvar

protected
def set_myvar
@myvar=‘abcdefg’
end

This probably isn’t kosher – controller variables should be set from
inside controllers, not layouts… but it’s neat enough that I wish
there were a good reason to use it… you could include this in your
layout:

<% @controller.instance_eval { @myvar=‘foo’ } %>


#7

#if animikii /* Jun 02, 12:11 */

Great, this works too and I’ll use it. I like that it doesn’t get set
until I call for the variable. The only thing is I can’t restrict it
to certain actions but for my purposes this doesn’t matter because I’m
only calling it from actions where I need it so that’s good enough.
Thanks,
Jeff

if you want to restrict access to certain actions then i suggest you
take a look at ActiveRBAC [0] which will allow you to quite easily
specify such restrictions, like this:

class MyController < ApplicationController
before_filter :protect_controller, :except => [ :list, :index, :show ]

def protect_controller_to_role( role )
if !session[:rbac_user_id].nil? and
User.find(session[:rbac_user_id]).has_role?( “Admin”, role )
return true
else
redirect_to ‘/q/list’
flash[:notice] = “You are not allowed to access this page”
return false
end
end

def protect_controller
protect_controller_to_role(“BasicUser”)
end
end

the above restricts access to all actions in the controller except for
list, index and show, unless they have been granted the Admin or
BasicUser role.

have a look at the manual [1] for more information on what can be done.

-v

[0] https://activerbac.turingstudio.com/trac
[1] https://activerbac.turingstudio.com/releases/ActiveRbacManual.pdf

    class ApplicationController < ActionController::Base
 type when getting the current user.
  1. mailto:removed_email_address@domain.invalid
  2. http://www.mdaines.com/
  3. mailto:removed_email_address@domain.invalid
  4. http://lists.rubyonrails.org/mailman/listinfo/rails

Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

#endif /* removed_email_address@domain.invalid */


keys: http://codex.net/gpg.asc

I did’nt notice when the world went to shit. I was watching TV.