开发者

Rails - Making @projects avaialble on all page loads

开发者 https://www.devze.com 2023-01-23 02:23 出处:网络
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?

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号