How to create block helpers in Rails?

Asked

Viewed 193 times

1

I need to create a dropdown menu helper... but I don’t have much idea how to do that. Wanted a block helper, as is done with forms. Ex.:

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

On 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 %>

One could give an example, or even a north of how it works?

1 answer

1

You must use a block parameter and get the contents of the block with the method capture, as an example:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.