演示者名称错误未初始化常量

时间:2015-01-07 00:22:37

标签: ruby-on-rails ruby

我使用以下RailsCast作为指南,开始为我的一些复杂视图实现演示者:http://railscasts.com/episodes/287-presenters-from-scratch

我收到以下错误NameError at /bids/41 uninitialized constant BidPresenter并且无法弄清楚我哪里出错了!更好的错误告诉我这条线导致帮助器中的错误:klass ||= "#{object.class}Presenter".constantize

# presenter helper
# https://www.youtube.com/watch?v=HWN1nUlgQ8Y
def present(object, klass = nil)
  # if a klass isn't specified determine the class based off the object 
  # klass (bids presenter, user presenter etc)
  klass ||= "#{object.class}Presenter".constantize
  # instantiate presenter
  presenter = klass.new(object, self)
  # yield presenter if a block is given
  yield presenter if block_given?
  # return presenter back from the method
  presenter
end

我的bids_presenter.rb

class BidsPresenter
    def initialize(bid, template)
        @bid = bid
        @template = template
    end

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

    def bid_data_upper_price_estimate
        h.number_to_currency(@bid.upper_price_estimate, precision: 2)
    end
end

我的观点show.html.erb

<% present @bid do |bid_presenter| %>
    <%= bid_presenter.bid_data_upper_price_estimate %>
<% end %>

1 个答案:

答案 0 :(得分:1)

您的课程名为BidsPresenter,但您的错误表明代码正在寻找BidPresenter

不是“s”。