e x p r e s s i c a

ruby on rails, business and technicalities

No public Twitter messages.

  • RSS
  • Facebook
  • Twitter
  • Linkedin

The view helper method capture provides this functionality to wrap a section of the view template in a variable and can be used multiple times in the view.

Usage :

<% @message = capture do %>
  You can earn a lot, if you use your brain!
  <%= link_to "Click here", earning_url %> to know more!
<% end %>


If its needed to render a section of template code, we usually go with partials. Capture is specifically useful when partial is only going to be used in just one view template. As there will be this variable available throughout the template, we can use it as many times as needed instead of partial.

3 Responses so far.

  1. sazwqa says:

    nice trick, useful !

  2. Arpit Jain says:

    Goood.. I didnt know that.
    Though I think partial is still a better way. :)

  3. Tom says:

    I use this to solve a problem I have – I don’t want to use many templates, and there are a few things I want to change outside of the so I just capture the parts I want to change and the template shows them there.
    is there a better way to do this?