如何在演示者中使用Enumerable mixin?

时间:2015-01-12 00:34:06

标签: ruby-on-rails ruby ruby-on-rails-4 enumerable

我有一个演示者,我想让each_with_index方法可用。我在我的基本演示者中添加了include Enumerable但是,我仍然得到一个no方法错误。我目前的代码如下:

index.erb

<% @bids.each_with_index do |bid, index| %>
  <% present bid do |bid_presenter| %>
     <div class="large-4 medium-4 columns <%= bid_presenter.last_column %>"></div>
  <% end %>
<% end %>

bid_presenter.rb

class BidPresenter < BasePresenter      
    presents :bid

    def last_column
        if index == bid.size - 1
            'end'
        end
    end
end

base_presenter.rb

class BasePresenter
  include Enumerable
    def initialize(object, template)
        @object = object
        @template = template
    end

private

    def self.presents(name)
        define_method(name) do
            @object
        end
    end

    # h method returns the template object
    def h
        @template
    end

    # if h is missing fallback to template
    def method_missing(*args, &block)
        @template.send(*args, &block)
    end
end

bids_controller.erb

  # GET /bids
  # GET /bids.json
  def index
    @bids = current_user.bids
  end

1 个答案:

答案 0 :(得分:0)

您要将所有未明确实施的方法转发给@template。无论@template是什么,似乎都没有正确实施each。您需要@template正确回复each