Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a helper in Ruby on Rails to capture Haml blocks?

I am writing a Rails helper method that will add wrapper html to captured content blocks and replace content_for method, such as

- content_for :header do
 //haml code

..would become

- content :header do
 //haml code

In order to do this I am using Haml and Ruby blocks. This is what it looks like

def content(name,&block)
 content_for name do
   capture_haml do
     haml_tag "div",{:id=>name.to_s} do
       haml_tag "div",{:id=>"#{name.to_s}_group"} do
         block
       end  
     end    
   end      
 end  
end

But I can't get this to work. There is no error message. It just doesn't show the block at all! I'm not sure what I am doing wrong. I'd appreciate a second opinion.

like image 731
picardo Avatar asked Aug 14 '10 23:08

picardo


1 Answers

You're doing roughly the right thing, but you aren't actually calling the block #content is passed. To do so, either use block.call, or Ruby's built-in yield statement.

like image 154
Natalie Weizenbaum Avatar answered Oct 13 '22 21:10

Natalie Weizenbaum