简体   繁体   中英

Render partial layout doesn't display anything

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.

<% @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.

You forgot to output your 'render' call:

<%= render ... %>

instead of

<% render ... %>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM