undefined Number_to_currency inside a helper(是视图或控制器的助手?)

undefined Number_to_currency inside a helper (are helpers for views or controllers?)

我有一个视图助手,但是当我尝试 cost_dollar 方法时得到一个未定义的方法“number_to_currency”。

app/helpers/books_helper.rb

Module BooksHelper
  require 'action_view'
  include ActionView::Helpers::NumberHelper 

  def cost_dollar
   @book.cost.number_to_currency
  end
end

你应该使用 number_to_currency(值) 而不是 value.number_to_currency

  def cost_dollar
   number_to_currency(@book.cost)
  end