I was hoping to use one overall layout at the application.rb level
(layout “site_layout”) plus have another layout within this at the
controller level, however I can’t seem to get this to work. If I do put
in a specific controller layout it seems to override the application.rb
level one.
So is it not possible therefore to have a nested layout in rails?
If no how are people handling an overall site layout (e.g. header,
footer) and then controller specific layouts which might say have a
specific layout for the navigation (say on left of content area) and
content area?
Tks - I’ve got content_for working ok. I can use a layout for the
overall
header/footer and nav/content area layout. In this layout file then:
a) In the Navigation area I use: <%= yield :navigation_content %> and
b) In the content area I use: <%= @content_for_layout %>
Within each of a controller view I use <%= render :partial =>
‘contact_navigation’ %> as I have to put this in each of the views for a
given controller. This way I can put the actual controller specific
navigation in a file such as _contact_navigation.rhtml.
The only problem here is that one has to put the an entry such as <%=
render
:partial => ‘contact_navigation’ %> in EACH of the views in that
controller
area. It would be beter to have this automatically occur, which I
guess is
getting back to the concept of having a layout at the controller level
too.
So a layout (controller specific) within a layout (the overall site
layout).
Is there a way to achieve this? (given that layouts within layouts
don’t
seem to work)
I was hoping to use one overall layout at the application.rb level
(layout “site_layout”) plus have another layout within this at the
controller level, however I can’t seem to get this to work. If I
do put
in a specific controller layout it seems to override the
application.rb
level one.
Chris - I guess what I’m after is a way to handle the navigation pane
without having to put knowledge of this (or any additional lines) into
each
of controllers view files, i.e. don’t want to have to put something into
list.rhtml, new.rhtml, edit.rhtml, show.rhtml each time (i.e. number of
controllers x about 4-5).
Seeing I’m already using the rails layout concept for my overall site
specific header/footer layout, to do what I want I guess I need a way to
inject a “layout” like concept in the controller itself, for example in
contact.rb (i.e. I guess I’m trying not to have to modify the outcome of
the
rails scaffold generated views etc). Would any of the ideas you were
suggested be able to do this?
That looks like a good technique for what you want to do. I just tried
it on my app but i get an undefined method error…
ActionView::TemplateError (undefined method `inside_layout’ for
#<#Class:0x32205cc:0x32205a4>) on line #1 of
app/views/layouts/store.rhtml:
Weird. The method is definitely defined and required in the
environment…
Oops, that was just my mistake… I had omitted the last couple of
lines to be included in environment.rb… The method works perfectly
now and has saved me from rendering the same partial about 20
times…
One option you could employ as an alternative to inserting render
partial lines in all of your views, is to add the following lines to one
of your controllers.
(this is a bit dirty but it works)
before_filter :side_bar
def side_bar
assigns[:side] = response.template.render(:partial => ‘side_bar’)
response.template.instance_variable_set(:@assigns_added, nil)
end
And then in your ‘layout’
<% if @side -%>
<%= @side %>
<%= yield %>
<% else -%>
<%= yield %>
<% end -%>
Yes this violates both encapsulation and MVC… but if you’re
comfortable doing that, this can save you from having to add lines like
this to your views:
<% @side = capture do -%>
<%= render :partial => ‘side_bar’ %>
<% end -%>
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.