Ruby on rails 3 Rails 3.1表格助手

Ruby on rails 3 Rails 3.1表格助手,ruby-on-rails-3,helper,Ruby On Rails 3,Helper,我正在尝试创建一个助手方法,以使我的视图干涸很多,这基本上会为我呈现一个表。到目前为止,我希望使用的语法是: = table_for @notes do = column "Date" do = time_ago(note.created_at) = column "Content" do = note.content 这是我到目前为止的助手: module TableHelper def table_for(items) @resour

我正在尝试创建一个助手方法,以使我的视图干涸很多,这基本上会为我呈现一个表。到目前为止,我希望使用的语法是:

 = table_for @notes do

  = column "Date" do
    = time_ago(note.created_at)

  = column "Content" do
    = note.content
这是我到目前为止的助手:

module TableHelper

    def table_for(items)
        @resource = items.singularize # Singularizes the resource
        @columns = []

        yield
        # Create a table
        content_tag :table, class: 'table table-striped' do
            thead + tbody(items)
        end
    end

    def thead # Loop through columns and print column label
        content_tag :thead do
            content_tag :tr do 
                 @columns.each do |c|
                 concat(content_tag(:th, c[:label]))
                 end
            end
        end
    end

    def tbody(items) # This bit fails :(
  content_tag :tbody do
    items.each { |e|
     concat(content_tag(:tr){
        @columns.each { |c|
          e[c[:label]] = c[:block].call(e[c[:label]]) if c[:block]
          concat(content_tag(:td, e[c[:block]]))
        }
      })
    }
  end
end

    def column (label, &block) # Takes a label and block, appending it to the columns instance
        @columns << { label: label, block: block}
        nil # Stops it printing the @columns variable
    end
end
模块TableHelper
def表_(项目)
@resource=items.singularize#对资源进行singularize
@列=[]
产量
#创建一个表
内容标签:表,类:'table table striped'do
THAD+T车身(项目)
结束
结束
def thead#遍历列并打印列标签
内容标签:thead do
内容标签:tr do
@列。每个列都有| c|
concat(内容标签(:th,c[:label]))
结束
结束
结束
结束
def tbody(项目)#此位失败:(
内容标签:tbody do
项目.每项{e|
concat(内容标签(:tr){
@每列{c|
e[c[:label]]=c[:block]。如果c[:block],则调用(e[c[:label]])
concat(content_标签(:td,e[c[:block]]))
}
})
}
结束
结束
def column(label和block)#获取标签和块,将其附加到columns实例

@列可能需要替换

= table_for @notes do` 

函数的表中,当您屈服时,您应该执行屈服项。这里的项是项数组的一个实例。因此也需要准备它


编辑:这个链接有一个不同的方法:

我不理解
@resource=items.singularize
表中的
用于
方法。你不是在
数组上调用
singularize
方法吗?我想它应该不起作用。请再解释一下好吗?你绝对正确,那不是我的意思这是以前的一次尝试,但如果你对如何使这项工作成功有任何建议,那就太好了!:)你是不是在尝试修改一些东西,而不是在你的问题中发布的。如果是,请更新你的问题,否则我会用一个链接更新我的答案,你可以试试。除此之外,我会在周末看看这个。
= table_for @notes do |note|