Rails - Making @projects avaialble on all page loads
On every page of my app I want to show a user's projects... So which controller do I use to make sure that @pr开发者_如何转开发ojects from the projects controller is being made available in the view?
Thanks
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:
- Action Controller Filters - Rails Guides
- ActionController::Filters - API
精彩评论