views:

34

answers:

1

For instance, i want to have my sidebar to have several dynamic content. Using other method will lead me to put query codes into View, which is not a good idea at all. I would like to keep any query in my Controller.

Currently as i know there are several ff. method:

Render a shared partial -> No where to put the query

render :partial => "shared/sidebar"

Content For -> Additional details in the comment

<%= yield :sidebar %>
<% content_for :sidebar do %>
  <a href="http://www.netscape.com"&gt;Netscape&lt;/a&gt;&lt;br&gt;
  <a href="http://www.lycos.com"&gt;Lycos&lt;/a&gt;&lt;br&gt;
  <a href="http://www.walmart.com"&gt;Wal Mart</a><br>
<% end %>

3rd is write it directly to the layout file.

So how should I make this work?

+3  A: 

IF you want this in every view, you can place the method that populates the necessary data in application_controller and use a before_filter to trigger it.

before_filter :load_sidebar

def load_sidebar
  @data = Thingy.find(:all)
end

Then your partial or content_for element checks for @data and processes.

Toby Hede