Ruby on rails logic for partial in layout

I have a sidebar that will have some logic in it, just as the view speaks to the controller. Where to put the logic for partial? Create a new controller for the layout and place it there?

Layout for registered users, for example, a toolbar. The dashboard will have a sidebar with the same dynamic content on each page. Since it is displayed on every page, I want to express it partially.

I'm just confused about where to put the find and all this information.

+7
ruby-on-rails layout partial-views
source share
3 answers

There may be a better way to do this, but if on most or all pages you can create before_filter on your ApplicationController. You will need to call skip_before_filter on controllers / actions that do not need data.

 class ApplicationController < ActionController::Base before_filter :load_sidebar_data protected def load_sidebar_data end end 

If you do not need this on most pages, you will still put this method in the ApplicationController, you just add a filter before where you need it.

+8
source share

Put the part in the layouts folder. The file name must begin with an underscore. For example, "_mypartial.html.erb".

Then in your views use the following code to include partial

"mypartial"%>

Please note that you do not include underscores in the ruby ​​inline code when specifying a partial display.

+1
source share

You can use cells (view components for Rails): http://cells.rubyforge.org/

+1
source share

All Articles