Ruby on rails rails中的多级块方法
我正在制作一个视图帮助器,以一种格式呈现一组数据。我上了这些课Ruby on rails rails中的多级块方法,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我正在制作一个视图帮助器,以一种格式呈现一组数据。我上了这些课 require 'app/data_list/helper' module App module DataList autoload :Builder, 'app/data_list/builder' @@data_list_tag = :ol @@list_tag = :ul end end ActionView::Base.send :include, App::DataLis
require 'app/data_list/helper'
module App
module DataList
autoload :Builder, 'app/data_list/builder'
@@data_list_tag = :ol
@@list_tag = :ul
end
end
ActionView::Base.send :include, App::DataList::Helper
助手是
module App
module DataList
module Helper
def data_list_for(object, html_options={}, &block)
builder = App::DataList::Builder
arr_content = []
object.each do |o|
arr_content << capture(builder.new(o, self), &block)
end
content_tag(:ol, arr_content.join(" ").html_safe, html_options).html_safe
end
end
end
end
列模块是
module App
module DataList
module Column
def column(attribute_name, options={}, &block)
collection_block, block = block, nil if block_given?
puts attribute_name
value = if block
block
elsif @object.respond_to?(:"human_#{attribute_name}")
@object.send :"human_#{attribute_name}"
else
@object.send(attribute_name)
end
wrap_list_item(attribute_name, value, options, &collection_block)
end
end
end
end
现在我编写代码来测试它
<%= data_list_for @contracts do |l| %>
<%= l.column :age %>
<%= l.column :contact do |c| %>
<%= c.column :phones %>
<% end %>
<%= l.column :company %>
<% end %>
一切正常,年龄
,联系人
,公司
工作正常。但是,联系人的电话
未显示
有人知道吗,我知道我在代码中遗漏了一些东西。正在寻找你的帮助
更新的问题和完整的来源是我可以在专栏模块中看到两个问题
1) 如果提供了块,则将其设置为nil-因此,如果块
始终返回false。2) 即使block
不是nil,也只是将块作为值返回,而不是实际将控制传递给块。您应该调用block.call
或。隐式块执行得更快,因此我认为您的列模块应该更像这样:
module DataList
module Column
def column(attribute_name, options={})
value = begin
if block_given?
yield self.class.new(@object.send(attribute_name), @template)
elsif @object.respond_to?(:"human_#{attribute_name}")
@object.send :"human_#{attribute_name}"
else
@object.send(attribute_name)
end
end
wrap_list_item(attribute_name, value, options)
end
end
end
在column模块中我可以看到两个问题 1) 如果提供了块,则将其设置为nil-因此,如果块始终返回false。2) 即使
block
不是nil,也只是将块作为值返回,而不是实际将控制传递给块。您应该调用block.call
或。隐式块执行得更快,因此我认为您的列模块应该更像这样:
module DataList
module Column
def column(attribute_name, options={})
value = begin
if block_given?
yield self.class.new(@object.send(attribute_name), @template)
elsif @object.respond_to?(:"human_#{attribute_name}")
@object.send :"human_#{attribute_name}"
else
@object.send(attribute_name)
end
end
wrap_list_item(attribute_name, value, options)
end
end
end
该解决方案现在发布在中。该解决方案现在发布在中。感谢您的帮助,但它产生了一些错误。在第
行中,它生成错误ActionView::Template::error(nil:NilClass的未定义方法
column):`现在错误已更改ActionView::Template::error(#的未定义方法
phones):`Hm,yield self.class.new(@object.send(attribute_name),@Template)
?我假设正在传递到的data\u list\u的联系人有一个contact
方法,该方法返回一个对象,该对象将响应电话
?抱歉,回复太晚了。你建议的改变现在起作用了。。。万岁!但是现在它也引起了一个小问题,:手机正在渲染,但是:cotnac
不渲染。我得多写一行<代码>
谢谢您的帮助,但它产生了一些错误。在第
行中,它生成错误ActionView::Template::error(nil:NilClass的未定义方法
column):`现在错误已更改ActionView::Template::error(#的未定义方法
phones):`Hm,yield self.class.new(@object.send(attribute_name),@Template)
?我假设正在传递到
的data\u list\u的联系人有一个contact
方法,该方法返回一个对象,该对象将响应电话
?抱歉,回复太晚了。你建议的改变现在起作用了。。。万岁!但是现在它也引起了一个小问题,:手机正在渲染,但是:cotnac
不渲染。我得多写一行<代码>