0

I have this helper method:

  def pick_index_paths_new



 case params[:type]
     when 'eood', 'ood'
     content_tag :li , :class => 'link_list' do
       link_to "New EOOD", new_eood_path 
       link_to "New OOD" , new_ood_path 
      end
     when 'llc' , 'ccompany' , 'scompany'
      content_tag :li, :class => 'link_list' do
        link_to "New LLC" , new_llc_path
        link_to "New C Company" , new_ccompany_path
        link_to "New S Company" , new_scompany_path
       end
     else link_to "New" , new_resource_path , :data => { :no_turbolink => true } 
 end

end

Here's how it looks I in the view:

 <div class="row">
<div class="container">
  <ul class="tabs">
     <%= pick_index_paths_new %> 
  </ul>
  <table class='table'>
      <thead>
        <tr>
  !-- some other stuff --

When I load my page, only the last link is visible (Either "New OOD" or New SCompany") .

I tried putting all the logic in my view, and it works properly there (but it is outright gruesome)

What am I doing wrong? What do I have to change so that the helper method will render all links?

Hristo Georgiev
  • 2,499
  • 1
  • 16
  • 23

1 Answers1

2

This is because in ruby the last call in a method is implicitly returned. You're calling content_tag with a block and then calling multiple things inside of it. The last call will be used as the implicit return value. To display all of the links you could build up an array of links and then join them (I also think you need to call html_safe), which is the implicit return of content_tag.

def pick_index_paths_new
  [].tap do |array|
    case params[:type]
      when 'eood', 'ood'
        array << link_to("New EOOD", new_eood_path)
        array << link_to("New OOD" , new_ood_path) 
      when 'llc' , 'ccompany' , 'scompany'
        array << link_to("New LLC" , new_llc_path)
        array << link_to("New C Company" , new_ccompany_path)
        array << link_to("New S Company" , new_scompany_path)
    else 
      array << link_to("New" , new_resource_path , data: { no_turbolink: true })
    end
  end.map do |link|
    content_tag :li, link, class: 'link_list'
  end.join.html_safe
end
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
  • Thank you, excellent knowledge! However, the do/end operator renders the whole array as a single list item. I made a clumsy workaround by adding list tags in between the links in the array like this : "
  • " ,link_to("New LLC" , new_llc_path) ,"
  • ", "
  • " , link_to("New C Company" , new_ccompany_path), "
  • ", – Hristo Georgiev Jul 18 '14 at 07:44
  • @HristoGeorgiev I've updated my answer to wrap each link in an
  • . Hope that's a little bit cleaner
  • – Kyle Decot Jul 18 '14 at 07:52