Reputation: 3566
The effect i'm looking for is to implement a reusable component for a html box. I've found that people seem to using something like the following example but it's just not working for me. I have:
# app/views/home/index.html.erb
top
<% render :layout => 'layouts/box' do %>
<p>Here is some content</p>
<% end %>
bottom
and
# app/views/layouts/_box.html.erb
inside box
<%= yield %>
The end result i get rendered on the browser is:
top bottom
Still, i can see in the logs:
Processing by HomeController#index as HTML
Rendered layouts/_box.html.erb (0.1ms)
Rendered home/index.html.erb within layouts/application (1.2ms)
So the box layout is getting processed. It's just not showing anything. Any ideas ?
I'm using Rails 3.1.3 and Ruby 1.9.2-p290.
Upvotes: 1
Views: 995
Reputation: 27463
Maybe you mixed up rendering partial and yielding content through content_for:
Rendering a partial:
# app/views/home/index.html.erb
top
<%= render 'layouts/box' %>
bottom
# app/views/layouts/_box.html.erb
Here is some content inside the box
# result:
top
Here is some content inside the box
bottom
Yielding content through content_for:
# app/views/home/index.html.erb
top
<% content_for :box do %>
Some content
<% end %>
bottom
# app/views/layouts/application.html.erb
<%= yield :box %>
<%= yield %>
# result
Some content
top
bottom
You can do:
# app/views/home/index.html.erb
top
<p>Here is some content</p>
<%= render 'layouts/box' %>
bottom
# app/layouts/_box.html.erb
inside box
In general, the layout folder is for the layouts (the container). If you need to use a partial across different controllers, place it in app/views/shared/_your_partial.html.erb.
More informations here: for yield and content_for: http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield
For partials: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
Upvotes: 0
Reputation: 2960
You forgot to output your 'render' call:
<%= render ... %>
instead of
<% render ... %>
Upvotes: 2
Reputation: 7078
<% @Var %>
hidden content
<%= @var %>
shows content
so it might be:
<%= render :layout => 'layouts/box' do %>
<p>Here is some content</p>
<% end %>
what you need.
Upvotes: 2