views:

32

answers:

1

Hello, On every page of my app I want to show a user's projects... So which controller do I use to make sure that @projects from the projects controller is being made available in the view?

Thanks

+4  A: 

In your application_controller do the following:

before_filter :load_projects

def load_projects
  @projects = Project.all
end

That will run the load_projects method on every request and populate the @projects variable.

You can also add conditions to your before filter like so:

before_filter :load_projects, :only => [:index]

def load_projects
  @projects = Project.all
end

See more about ActionController filters:

Patrick Klingemann
if I have 2 before_filters. What's right.. Added two lines of before_filter or commas?
TheExit
either, or... you need to be able to have multiple lines of before_filters so you can add conditions, etc. to them. I'll add an example in the above answer.
Patrick Klingemann