我有一个类多级块方法产生问题
class DataListBuilder
include ActionView::Helpers::TagHelper
include ActionView::Helpers::CaptureHelper
include ActionView::Helpers::UrlHelper
attr_accessor :object, :output_buffer
def initialize(object)
@object, @output_buffer = object, nil
end
def column (&block)
if block_given?
content_tag(:li, block.call(self))
else
content_tag(:li, "")
end
end
def options_column(&link_block)
if block_given?
content_tag(:li, content_tag(:dl, "<dt><a href='#'> </a></dt><dd><ul>#{link_block.call(self)}</ul></dd>".html_safe, :class=>'options'))
else
content_tag(:li, "")
end
end
def link_item(title, url, options={})
content_tag :li, link_to(title, url, options)
end
end
,把它作为
<%= l.options_column do |c| %>
<%= c.link_item 'Show', lead_path(c.object) %>
<%= c.link_item 'Edit', edit_lead_path(c.object) %>
<%= c.link_item 'New Note', "leads/#{c.object.id}/notes/new", :class=>"display-newxdoc", :id=>c.object.id %>
<%= c.link_item 'Create Opportunity', new_lead_opportunity_path(c.object) %>
<% end %>
期望的输出是
<li><dl class="options"><dt><a href="#"> </a></dt><dd><ul style="display: none;">
<li><a data-remote="true" class="plus" href="leads/details/309"> </a></li>
<li>3w</li>
<li>Simon Wu</li>
<li>1-714-553-0888</li>
<li>[email protected]</li>
<li>Unified Beat</li>
<li><a href="/leads/309">Show</a></li>
<li><a href="/leads/309/edit">Edit</a></li>
<li><a id="309" class="display-newxdoc" href="leads/309/notes/new">New Note</a></li>
<li><a href="/leads/309/opportunities/new">Create Opportunity</a></li>
但它产生
<li><a href="/leads/309">Show</a></li>
<li><a href="/leads/309/edit">Edit</a></li>
<li><a id="309" class="display-newxdoc" href="leads/309/notes/new">New Note</a></li>
<li><a href="/leads/309/opportunities/new">Create Opportunity</a></li>
<li><dl class="options"><dt><a href="#"> </a></dt><dd><ul style="display: none;">
<li><a data-remote="true" class="plus" href="leads/details/309"> </a></li>
<li>3w</li>
<li>Simon Wu</li>
<li>1-714-553-0888</li>
<li>[email protected]</li>
<li>Unified Beat</li>
<li><a href="/leads/309">Show</a></li>
<li><a href="/leads/309/edit">Edit</a></li>
<li><a id="309" class="display-newxdoc" href="leads/309/notes/new">New Note</a></li>
<li><a href="/leads/309/opportunities/new">Create Opportunity</a></li>
</ul></dd></dl></li>
</ul></dd></dl></li>
任何人都可以帮助我。
已完成代码here。
真的真的非常感谢。有用。这也让我完全了解正在发生的事情和内在的事情。现在唯一的问题是'lead_path(object)'和路由方法不起作用。它产生错误'ActionView :: Template :: Error(未定义的方法'lead_path'为#):'。 这是唯一留在列表中的东西。请帮忙解决它。 –
2011-04-07 07:02:59
@Nazar Huaain - 我更新了答案,以解决您的问题与链接。将'Rails.application.routes.url_helpers'和属性'controller'添加到'DataListBuilder'中。在视图中添加了'object.id'。 http://stackoverflow.com/questions/341143/can-rails-routing-helpers-i-e-mymodel-pathmodel-be-used-in-models/5456103#5456103了解更多信息 – 2011-04-07 07:39:49
非常感谢。这解决了我的问题。 – 2011-04-07 08:53:05