如何创建用于渲染'some_partial',:collection的类

时间:2012-08-28 11:16:00

标签: ruby-on-rails ruby-on-rails-3

我尝试使用包含的Enumarable创建ruby类 所以insode视图我尝试使用每个方法的渲染类,并且应该返回为特定content_id构建的对象

以下是使用此代码 some_file.haml

的视图
.
.
.
%table{:border => "0"}
  %thead
    %tr
      %td Files
      %td Views
      %td Comments
      %td Likes 
  %tbody
    - render 'content_stats_row', :collection => @graphs.content_stats_table, :as => :row
.
.
.

以下是 @graphs

的课程
module FusionGraphs
  class ContentStatsTable
    include Enumerable

    def initialize(content_ids, start_date, end_date)
      @content_ids, @start_date, @end_date = content_ids, start_date, end_date 
    end

    def summarized_content
      @summarized_content ||= Hash[ Content.where(:id => @content_ids).value_of(:id, :name) ]
    end

    def summarized_comments
      @summarized_comments ||= Comment.where(:content_id => @content_ids)
                                  .where('date(created_at) between ? and ? ', @start_date, @end_date)
                                  .group(:content_id)
                                  .count
    end

    def summarized_views
      @summarized_views ||= View.where(:viewable_type => 'Content', :viewable_id => @content_ids)
                            .where('date(created_at) between ? and ? ', @start_date, @end_date)
                            .group(:viewable_id)
                            .count

    end

    def summarized_likes
      @summarized_likes ||= Like.where(:content_id => @content_ids)
                            .where('date(created_at) between ? and ? ', @start_date, @end_date)
                            .group(:content_id)
                            .count
    end

    def each
      @rows ||= @content_ids.map do |content_id|
              OpenStruct.new( :content_name => summarized_content[content_id],
                              :views => summarized_views[content_id] || 0,
                              :comments => summarized_comments[content_id] || 0,
                              :likes => summarized_likes[content_id] || 0
                            )
             end
    @rows.each {|row| yield row }
    end

  end
end

我无法从** render'content_stats_row'得到结果,:collection => graphs.content_stats_table,:as => :行** 它不起作用 我做错了什么?

1 个答案:

答案 0 :(得分:0)

在你的haml视图文件中,你在渲染之前放置了( - )连字符,它应该是(=)相等。