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?
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 [email protected] link: http://guides.rubyonrails.org/layouts_and_rendering.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With