我有一個類多級塊方法產生問題
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