How to create block helpers in Rails?

1

I need to create a dropdown menu helper ... But I have no idea how to do this. I wanted a helper in block form, as it is done with forms. Ex.:

<%= form_for(@teste) do |f| %>
    <%= f.text_field :um_campo %>
<% end %>

In my helper, I wish I could do something like this:

<%= dropdown_menu, class: 'teste' do %>
    <%= menu_item "Teste", "fa fa-icon", teste_path %>
    <%= menu_item "Teste", "fa fa-icon", teste_path %>
    <%= menu_item "Teste", "fa fa-icon", teste_path %>
<% end %>

Could someone give an example, or even a north of how does this work?

    
asked by anonymous 16.12.2014 / 21:45

1 answer

1

You should use a block parameter and get the contents of the block with the capture , for example:

def dropdown_menu(options = {}, &block)
   content_tag(:ul, options) do
     capture(&block)
   end
end
    
17.12.2014 / 11:54