views:

358

answers:

3

I have a sidebar that is going to have some logic in it, similar to how a view talks to a controller. Where do I put the logic in for the partial? Do I create a new controller for the layout and put it in there?

The layout is for logged in users, like a dashboard. The dashboard is going to have a sidebar that shows the same dynamic content on every page. Since it's being shown on every page I want to put it in a partial.

I'm just confused on where to put the find's and all that information.

A: 

Put the partial in the layouts folder. The name of the file should start with an underscore For example, "_mypartial.html.erb".

Then, in your views, use the following code to include the partial

<%= render :partial => "mypartial" %>

Note that you do not include the underscore in the embedded ruby code when specifying which partial to render.

cakeforcerberus
+3  A: 

Maybe there's a better way to do this, but if it's on most or all pages you could create a before_filter on your ApplicationController. You'll have to call skip_before_filter on the controllers/actions that don't need the data.

class ApplicationController < ActionController::Base
  before_filter :load_sidebar_data

protected
  def load_sidebar_data

  end
end

If you don't need it on most pages you'd still put the method in the ApplicationController, you'd just add the before filter where you need it.

Andy Gaskell
Andy, you've been a huge help to me these past two days! Thanks!
Ryan
A: 

You might consider using Cells (view components for Rails): http://cells.rubyforge.org/

Jason Galvin