Forum: Ruby on Rails Set instance variable for all actions in the Controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3036c74b9c278dc525af5692080a6d80?d=identicon&s=25 Jeff Ward (Guest)
on 2006-06-02 03:24
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-06-02 03:30
(Received via mailing list)
Hi --

On Thu, 1 Jun 2006, Jeff Ward 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 :-)

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 (dblack@wobblini.net)
* Ruby Power and Light, LLC (http://www.rubypowerandlight.com)
   > Ruby and Rails consultancy and training
* Author of "Ruby for Rails" from Manning Publications!
   > http://www.manning.com/black
3036c74b9c278dc525af5692080a6d80?d=identicon&s=25 Jeff Ward (Guest)
on 2006-06-02 03:42
(Received via mailing list)
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.
90842970ceaf7ed19136ce7b6f8e1455?d=identicon&s=25 Michael Daines (Guest)
on 2006-06-02 05:57
(Received via mailing list)
>   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 Daines
http://www.mdaines.com
3036c74b9c278dc525af5692080a6d80?d=identicon&s=25 Jeff Ward (Guest)
on 2006-06-02 09:14
(Received via mailing list)
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
7f30149100d05b8869b0ca351ef9e756?d=identicon&s=25 Vincent AE Scott (Guest)
on 2006-07-28 13:10
(Received via mailing list)
#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/Activ...



>         class ApplicationController < ActionController::Base
>      type when getting the current user.
>    1. mailto:michael.daines@gmail.com
>    2. http://www.mdaines.com/
>    3. mailto:Rails@lists.rubyonrails.org
>    4. http://lists.rubyonrails.org/mailman/listinfo/rails

> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

#endif /* animikii@gmail.com */

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

 I did'nt notice when the world went to shit.  I was watching TV.
10ac464635462f51a3b844aecf500bfe?d=identicon&s=25 Robert Mela (Guest)
on 2006-08-16 05:07
(Received via mailing list)
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' } %>
This topic is locked and can not be replied to.