演示者展示页面

Presenter show page

我已开始将所有视图逻辑移至演示者,但我在索引视图中收到以下错误 NameError at /bids uninitialized constant Bid::ActiveRecord_Associations_CollectionProxyPresenter。演示者在除 index.js 之外的所有其他视图中工作。我已经添加了我尝试过的修复的注释代码。

代码:

索引视图:

<% present @bids do |bid_presenter| %>

<% end %>

# tried @bids to match controller
# <% present @bids do |bid_presenter| %>
# <% end %>

bid_presenter.rb:

class BidPresenter < BasePresenter
    presents :bid

    # tried :bids
    # presents :bids
end

base_presenter.rb:

class BasePresenter
    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

    def method_missing(*args, &block)
        @template.send(*args, &block)
    end
end

bids_controller.rb:

def index
    @bids = current_user.bids
end

你试过了吗:

<% @bids.each do |bid| %>
 <% present bid do |bid_presenter| %>

 <% end %>
<% end %>

Presenter 正在展示模型的一个实例;您的代码试图显示 ActiveRecord::Collection 或其他内容。