Question

In a customer helper I need to add some radio buttons and occasional images.

I can brute-force it via direct html, or using content_tag(:input ...) but is there any way to use existing helpers such as radio_button_tag inside of instead of a content_tag in a helper method?

Was it helpful?

Solution

Yo can do something like this in a helper method. Even pass a block with html

some_helper.rb

def nav_dropdown_item label
    return nav_item(label, "#") unless block_given?
    content_tag(:li, class: 'dropdown') do
        link_to(t(label), "#", class: 'dropdown-toggle', data: {toggle: 'dropdown'}) +
        content_tag(:ul, yield, class: 'dropdown-menu')
    end
end

some_view.html.erb

<%= nav_dropdown_item 'menu.admin.title' do %>
  <% nav_item('menu.admin.chains', '/chains') +
  nav_item('menu.admin.food_types', '/food_types') +
  nav_item('menu.admin.street_types', '/street_types') +
  nav_item('menu.admin.poi_sources', '/poi_sources') +
  nav_item('menu.admin.poi_types', '/poi_types') %>
<% end %>
<%= nav_item 'menu.users', users_path %>

OTHER TIPS

Example from one of my projects

def apply_button(offer)
  ... a lot of code here ...

  link_to span.html_safe, offer_applications_path(offer), method: :post, remote: true, id: 'offer-apply-button'
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top