Defining layouts and application.rhtml

Why do I lose the application.rhtml template when I define a layout in a
controller? This seems to violate DRY.

The same problem also occurs when using a general layout for a
controller, such as app/views/layouts/data.rhtml for DataController.

Because a layout that corresponds to your controller name allows you to
override the default application layout. Or conversely, any controller
without a layout defined automatically inherits the application layout.
You are not required to create a layout for each controller. Heck you
don’t even have to create a view template. You can render directly to
the response that is sent to the user agent. For example, render :text
=> ‘Hello World!’ in an action will spit out Hello World!.

On 9/22/06, Carl J. [email protected] wrote:

Why do I lose the application.rhtml template when I define a layout in a
controller? This seems to violate DRY.

I haven’t used it, but this looks like what you want to do:

http://nested-layouts.rubyforge.org/

That said, I think it should also be possible to rethink your layout
and use partials to avoid RY.

-Pawel

You don’t have to do that. Just don’t create layouts for your
controllers.
Just view templates for your actions. The view templates will
automatically
use the application layout.

OK. Well, I’m trying to follow DRY but running into problems with my
site’s header and footer. Every single page of my site will have the
same header and footer. I have that in application.rhtml right now. It
looks like I will have to create _header and _footer partials and
include those instead when I have shared layouts. Not a lot of repeating
though… I’m probably just being too picky. :slight_smile: Thanks!

I’m a newbie, maybe I’m missing something… I know if I have an action
“browse” in DataController that it will use the template
app/views/data/browse.rhtml. But I want that “browse” template to be
used for a bunch of actions in multiple controllers. That’s why I tried
to simply call it “standard” and define the layout in each controller.

define app/views/layouts/application.rhtml
define app/views/<my_share_folder>/_<shared_partial>.rhtml
define app/views//.rhtml

within app/views//.rhtml file render the shared
partial
like so

render(:partial => ‘<my_share_folder>/<shared_partial>’, …)

Your action will get the appropriate view, embed the shared partial, and
use
the application layout to wrap the templates. If you include a path to
the
partial, app/views is considered the root, otherwise Rails looks in
app/views/.

I suggest reading Agile Web D. with Rails. This is
specifically
covered in the book.

On 9/22/06, Carl J. [email protected] wrote:

Thanks for your help!

Carl


I was struggling a bit with layouts as well. I new about partials ,
but then I read up on the @content_for . That is pretty powerful. In
one layout I can have multiple partials whereever I want. Check the
api (sorry don’t have the link)
As an example though -
In my view I have this , with the html and code between -
<% content_for ‘postform’ do -%>
<% end-%>

then in my layout i just decide where that goes and put the
@content_for_postform

Hope this helps and is relevant.

Stuart

Yeah, I understand the logic, just having trouble wrapping my head
around what I’m trying to do I guess. I have various little “info boxes”
that pull data from different controllers, and these will be on the side
of various screens. Sort of a portal view.

I think my problem is that I am using partials for data display only. If
I use a shared partial for the standard HTML template as well, that
ought to accomplish what I need.

Thanks for your help!

Carl

On 9/23/06, Dark A. [email protected] wrote:

ought to accomplish what I need.
As an example though -
In my view I have this , with the html and code between -
<% content_for ‘postform’ do -%>
<% end-%>

then in my layout i just decide where that goes and put the
@content_for_postform

Hope this helps and is relevant.

According to the documentation, your call to @content_for_postform is
deprecated. It says that the preferred way now is to use <%= yield
:postform %>.

– James