Question

I new to Ruby on Rails, and I am coming from Drupal/PHP background.

I am trying to do something simple. A "Static" controller for some pages like about us, disclaimer etc. and a "Video" controller for videos.

The whole application has same look. I am trying to figure out how I can avoid duplicating views.

In drupal the theming goes like this: html.tpl.php (renders page.tpl.php) - page.tpl.php (renders multiple block.tpl.php) - block.tpl.php (renders more nested tpls or some html generated by theme functions).

I find this to be great.

In rails, what I read is there is application.html.erb (Which I think is similar to html.tpl.php of Drupal, as it normally contains stylesheets, js, etc. available throughout the application) called "Layout". Then there are "Views", which "yield" content in this layout. We specify the view in controller, or it gets one automatically. I think of a view as "page.tpl.php" of Drupal.

Most of my site uses same structure i.e. header, top menu, content, right sidebar, footer. But content of each area changes depending on the path. So I am thinking of doing it like this:

  • Both 'Static' & 'Video' will call the same view, with different variable values i.e. values for content area and sidebar.
  • This View will the use 'render' to render another view, with a subset of variables passed from controller i.e. it will render content & sidebar views.
  • If needed, those views will render more views, and so on.

Another one I read about is partials. I can't seem to differentiate between rendering a view inside another view, and rendering a partial.

Which method is better? Or is there a better "Rails Way"?

Was it helpful?

Solution

This is quite a large topic to cover but edited highlights are

If you want to repeat content on every page, like a header or navbar for example then put it in layouts/application.html.erb

if you want to keep your views tidy with content specific to that page then you can use partials. Normally in views I create a folder called shared and then put my partials in there. for example if i save a partial called _form.html.erb (make sure you name partials with the underscore at the beginning of the file name), i would then call that in my view like

<%= render 'shared/form' %>

you can also use partials in the layouts/application file to keep that clean aswell

This is just my way of doing it, I'm sure more experienced rails guys have better ways

Hope that helps, any more questions then just ask

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top