我已经开始将所有视图逻辑移动到演示者,我在索引视图中收到以下错误NameError at /bids
uninitialized constant Bid::ActiveRecord_Associations_CollectionProxyPresenter
。除了索引之外,演示者在该模型的所有其他视图中工作。我添加了已经尝试过的注释修复代码。
代码:
索引视图:
<% 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
答案 0 :(得分:0)
你试过了吗?
<% @bids.each do |bid| %>
<% present bid do |bid_presenter| %>
<% end %>
<% end %>
Presenter正在展示该模型的一个实例;你的代码试图呈现一个ActiveRecord :: Collection等等。