我有一个辅助方法可以为某些 Controller 创建导航链接。
def gen_associations(controllers)
content_for :leftnav do
sorted_controllers = controllers.sort
returning String.new do |content|
content << content_tag(:h3, "Associations") <<
content_tag(:ul, :class => "nav") do
sorted_controllers.collect do |c|
content_tag("li", :class => ("last" if c == sorted_controllers.last)) do
link_to(c.humanize, eval("admin_#{c}_url"))
end
end
end
end
end
end
我不喜欢这种深度嵌套的结构,还有多余的<<
和其中一行的结尾。
我怎样才能重写它而不是像那样嵌套(更少的行)并且没有长行(<80 个字符)?
最佳答案
使用 partial - 将所有内容都放在返回闭包中,然后使用 render :partial => ...
关于ruby-on-rails - 如何让这个 ruby 方法不那么难看(嵌套),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1582343/