简体   繁体   中英

What is the most effecient way to get and view commonly used data inside a Rails application?

I have three models all associated with eachother via has_many :through method.

ProgramCategory
ProgramSubcategory
Program

Inside my application, I need to use ProgramCategory.title, ProgramSubcategory.title and Program.title very often. Lets say, there'll be a dynamic sidebar menu and it will look like this:

|- Shows (ProgramCategory)
  |- Action (ProgramSubcategory)
    |- Lost (Program)
    |- Games of Thrones (Program)
    |- Dexter (Program)

Since I'm aware of power of application_controller , application_helper and partials ; I feel lost about combining all of these together to find the most appropriate way.

Where and how should I call my models? Where should I build my methods to have access it through all of my controllers? Should I simply create a partial and render it at application layout?

I need some expert enlightment, please...

Thanks.

If this sidebar shows in every view throughout your application, you can it to your application layout. Then add a before filter in your application controller to get the data, this will get executed for each action in every controller that inherits from application controller. You can also restrict it to specific actions, only :index , and :show (for each controller) for example.

class ApplicationController < ActionController::Base
  before_filter :get_side_bar, :only => [:index, :show]

  def get_side_bar
    #@sidebar = some code
  end
end

Then you can use helper method (if needed) and render @sidebar in your application layout.

If you need to skip this action for some controllers you can do that as well, this one will skip application controller before filter for anything but index inside of the OtherController:

class OtherController < ApplicationController
  skip_before_filter :get_side_bar, :except => [:index]
end

This navigation bar is not a core part of the data you are showing, it will be more or less the same for all pages. Thus, it does not belong to the controller.

Make it a helper method, and cache its results in the view:

app/helpers/sidebar_helper.rb

module SidebarHelper
  def sidebar_data
    # blahblah, use any tag helper (include it here if necessary)
    # just remember to 
  end
end

app/controllers/your_controller.rb

class YourController < ApplicationController
  helper :sidebar
  # ...

(or put the helper method in your application helper to have it available everywhere)

app/views/application/_sidebar.html.haml

- cache do
  # well, put here whatever you need to output the sidebar
  # use the sidebar_data, which should return precooked data on 
  # which you can build your nested <ul>s.
  # just indent it two spaces, so it's inside the "cache do" block

or app/views/application/_sidebar.html.erb

<% cache do %>
  same as above, don't worry about indentation
<% end -%>

and include the partial where appropriate

<%= render 'sidebar' %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM