Matthias Hryniszak
Matthias Hryniszak

Reputation: 3162

Creating content for rails-based applications

I'm facing a problem of cleaning up my application in Ruby on Rails. What I have is a pretty standard 3-panel, header and footer layout where different parts of the screen contain different functionality.

By that I mean for example that the header contains (among others) a select that allows one to select parts of the application and a context-dependent menu. The main content area contains obviously the most interactive stuff whereas side panels contain quick-links with stuff like shopping-cart preview, list of potentially attractive products for the customer, a selector to narrow down the list of options...

I was wondering how do I go about simplifying the design. Right now I have the stuff that provides data for the "common" stuff (as opposed to direct content that's placed in the center) called from all the actions (with a filter) but that doesn't feel right for me. I've read that "components" are also not the way to go for obvious performance reasons.

Is there something that's more like component-oriented (other frameworks do have that kind of stuff - Grails: <ui:include ../>, ASP.NET MVC: <% Html.RenderAction() %>)?

Best regards, Matthias.

Upvotes: 0

Views: 112

Answers (2)

ohho
ohho

Reputation: 51941

layout

In app/views/layouts, a global layout application.html.erb defines the outer most layout (e.g. your 3-panel design).

The application layout can be overrided by a controller layout. For example, if you have a Tasks controller, you can add a tasks.html.erb in the app/views/layouts folder. The tasks.html.erb will be your default layout for any Tasks views (index, add, edit, etc.)

In application.html.erb or tasks.html.erb, you may add two partials, e.g. _header.html.erb, _side.html.erb. Those two partial will serve as the default panels for the views.

A better tutorial is at http://guides.rubyonrails.org/layouts_and_rendering.html

Upvotes: 0

Harish Shetty
Harish Shetty

Reputation: 64363

You can use content_for for managing different location of a layout.

layout.html.erb

<body>
  <div id="header">
  </div>

  <div id="content">

    <div id="main">
      <%= yield %>
    </div>

    <div id="side_bar">
      <%= yield :side_bar %>
    </div>
  </div>

  <div id="footer">
  </div>

</body>

In your views use content_for to assign content to various sections of the layout:

<% content_for :side_bar do %>
  <div>
    <h1> Popular Posts </h1>
  </div>
<% end %>

<div>
  <h1> All Posts </h1>
  <% @posts.each do |post| %>
    <h2> <%= post.name %> </h2> 
  <% end %>  
</div>

Reference:

Railscast

Edit 1

You can use partials to create reusable screen sections.

views/shared/_shopping_cart.html.erb

<div>
  display the shopping cart details
<div>

views/posts/index.html.erb

<% content_for :side_bar do %>
  <%= render :partial => 'shared/shopping_cart' %>
<% end %>

Essentially you can use:

  • Filters to set data
  • content_for to set layout
  • partials to display data

Combination of these three should allow you to componentize your solution.

Upvotes: 3

Related Questions