Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails logic for a partial in a layout

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.

like image 489
Ryan Avatar asked Aug 21 '09 16:08

Ryan


2 Answers

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.

like image 126
Andy Gaskell Avatar answered Oct 05 '22 04:10

Andy Gaskell


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

"mypartial" %>

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

like image 31
cakeforcerberus Avatar answered Oct 05 '22 05:10

cakeforcerberus