68

I have read it from the Rails guides, Have looked at Micheal Hartel book and now reading it from Rails View book but still I get confused :(

There is a _footer.html.erb file so it is a "partial" and in the code it has written:

<%=render 'layouts/footer' %>

so my understanding is that when it sees this, goes and insert the HTML for footer file in here. Ok... Now a few pages later it is saying:

<%= render partial: 'activitiy_items/recent' %>

so WHY this time we have the word "partial" in here but we didn't have it in the previous one?

And there somewhere else I see <%= yield :sidebar %>

So this yield also insert HTML in its place? Well wasn't it what render was doing?

I was hoping if another programmer instead of books explains this to me maybe I get it this time:)

2 Answers2

133

render & render partial:

  • render 'some_view' is a shorthand for render partial: 'some_view'.

  • render file: 'view' will look for a file view.html.erb and NOT _view.html.erb (.erb or any other renderer you use)

  • render can pass local variables for the partial if you do not use collections or layouts, like

     render 'some/path/to/my/partial', custom_var: 'Hello'
    

yield & content_for

  • yield is typically used in layouts. It tells Rails to put the content for this block at that place in the layout.
  • When you do yield :something associated with content_for :something, you can pass a block of code (view) to display where the yield :something is placed (see example below).

A small example about yield:

In your layout:

<html>
<head>
 <%= yield :html_head %>
</head>
<body>
 <div id="sidebar">
   <%= yield :sidebar %>
 </div>
</body>

In one of your view:

<% content_for :sidebar do %>
  This content will show up in the sidebar section
<% end %>

<% content_for :html_head do %>
  <script type="text/javascript">
    console.log("Hello World!");
  </script>
<% end %>

This will produce the following HTML:

<html>
<head>
  <script type="text/javascript">
    console.log("Hello World!");
  </script>
</head>
<body>
 <div id="sidebar">
   This content will show up in the sidebar section
 </div>
</body>

Posts that might help:

Links to documentation & guides:

Jeff Spicoli
  • 280
  • 5
  • 13
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • Further to his answer look at this for reference please: http://guides.rubyonrails.org/v2.3.8/layouts_and_rendering.html#using-partials – Deej May 29 '13 at 20:09
  • Ok thanks but still whats the difference between Render and Yield in a layout? They both insert HTML ? –  May 29 '13 at 20:14
  • So both Render and Yield are rendering HTML and inserting it? –  May 29 '13 at 20:19
  • 1
    `yield :something` will call `render` on the block of code provided in a `content_for :something`. `render` will just produce the HTML when it is called. – MrYoshiji May 29 '13 at 20:21
  • 1
    `yield` is a Ruby keyword - it is not specific to Rails. I had a hard time wrapping my head around it when I came from Java and PHP a year ago. – Niels B. May 29 '13 at 20:25
  • 1
    `render :partial 'some/path/to/my/partial'` should be `render partial: 'some/path/to/my/partial'` – Eric H. Mar 08 '14 at 00:19
  • 9
    You can pass variables to render without :partial, at least in Rails 4. Eg. render 'some/path/to/my/partial', custom_var: 'Hello'. See 'Rendering the default case' here: http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html – Patrick Cullen Apr 14 '17 at 20:41
4

About render , render :partial and yield

  • render :template and render :partial are two files in rails..


    render :template are mostly created according to an action with syntax demo.html.erb

    render :partial are reuseable and called from different views , are shared among many pages in application and syntax is _demo.html.erb

  • yield and render..


Yield is a way to call a block of code with its output but render will include a partial page template where it is called. In rails yield is mostly used in layout whereas render is used in actions or their templates

Don Cruickshank
  • 5,641
  • 6
  • 48
  • 48
Saumya Mehta
  • 358
  • 3
  • 15
  • 1
    This is grossly Incorrect regarding syntax of render :partial - render :partial is an expanded form and accepts local variables as MrYoshiji wrote above .. The "\_" is for partials that are NOT actions .. all actions (defined in controller) are written without the prefix ("_") -- You can call render on any defined singular/plural model name using the rails convetions – John Doe Jul 16 '16 at 16:55