SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Rails] global var?

    Hi,

    What's the best way to add a global var (I know it's bad).
    I have an Agency model with properties like phone and I need to display on each page agency.phone and agency.address. I moved the footer in a partial so the _footer.rhtml looks like

    How to get the @agency visible from everywhere? I thought Application is made for this but don't know howto do it...

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You put a before_filter in the app/controllers/application.rb:

    Code:
    ApplicationController < ActionController::Base
      before_filter :set_agency
    
    protected
    
      def set_agency
        @agency = Agency.find(1)
      end
    end
    (or other code if you want to set the var to something else ;-))

  3. #3
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks again Fenrir2, I'll have to add your name to the credits

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem, but that would be cool :P.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •