1

I write a block method to print a list

def column (&block)
  if block_given?
    content_tag(:li, capture(self, &block))
  else
    content_tag(:li, "")
  end
end

and using it as

<%= data_list_for @leads, [" :10", "Age:30", "Contact:140", "Phone:140", "Email:180", "Company:100", ""] do |l| %>
    <%= l.column { |c| link_to "&nbsp;".html_safe, "leads/details/#{c.object.id}", :class=>:plus, :remote=>true } %>
    <%= l.column { |c| c.object.age } %>
    <%= l.column { |c| c.object.contact.complete_name } %>
    <%= l.column { |c| c.object.contact.phones.blank? ? "-" : c.object.contact.phones.first.phone_number } %>
    <%= l.column { |c| c.object.contact.emails.blank? ? "-" : c.object.contact.emails.first.email } %>
    <%= l.column { |c| c.object.company.title } %>
    <%= l.column do |c| %>
        <%= options_menu do |m| %>
            <%= m.item link_to 'Show', lead_path(c.object) %>
            <%= m.item link_to 'Edit', edit_lead_path(c.object) %>
            <%= m.item link_to 'New Note', "leads/#{c.object.id}/notes/new", :class=>"display-newxdoc", :id=>c.object.id %>
            <%= m.item link_to 'Create Opportunity', new_lead_opportunity_path(c.object) %>
        <% end %>
    <% end %>
<% end %>

Every thing is working fine. But the only problem is that options_menu is generating twice. Means two option_menus are there. I traced out one menu is from l.column command because it has proper formating of column, other is generating by its on block in the view, How can i stop it for doing twice?

Nazar Hussain
  • 5,102
  • 6
  • 40
  • 67

3 Answers3

0

Remove = from the line

<%= options_menu do |m| %>
Yossi
  • 11,778
  • 2
  • 53
  • 66
0

I wouldn't use capture if the block is direct (not a "view block"):

content_tag(:li, block.call(self))
tokland
  • 66,169
  • 13
  • 144
  • 170
  • I tried this, i tried every thing but still can not figure out why its giving double output. – Nazar Hussain Apr 06 '11 at 05:08
  • I guess you also tried without the = in <%= data_list_for (that's for sure, block views do not use =). Can you show the code for this helper method? – tokland Apr 06 '11 at 07:05
  • Complete code is listed in this [link](https://gist.github.com/905258) and updated problem is listed in this [question](http://stackoverflow.com/questions/5562658/multi-level-block-method-is-generating-issue) – Nazar Hussain Apr 06 '11 at 07:15
0

The solution is now posted in the discussion.

Community
  • 1
  • 1
Nazar Hussain
  • 5,102
  • 6
  • 40
  • 67