I'm trying to understand how html.erb files work, and I am a little confused about the render and yield commands, as they both seem to be ways to make the filer cleaner and simpler by substituting in code from other html.erb files. Can someone explain to me the difference between render and yield?
Asked
Active
Viewed 5,552 times
1 Answers
6
render
is used for invoking a partial page template whereas yield
is used a placeholder where you want the output of your templates to yield their content. So you use render when building up the content, and yield to show the content in essence.
As a general rule of thumb, yield
is used in the 'layout' level templates (in the most basic example, the application.html.erb in the /app/views/layout directory). Render is used in your resource/action specific templates.
Also take a look at the content_for
tag (block) and how you can use it to further break up your application-level templates into sections.
Obligatory guides@rubyonrails.org link: http://guides.rubyonrails.org/layouts_and_rendering.html

colinross
- 2,075
- 13
- 10