首页 > 解决方案 > 帮助器中未定义的 Number_to_currency (是视图或控制器的帮助器吗?)

问题描述

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

应用程序/helpers/books_helper.rb

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

  def cost_dollar
   @book.cost.number_to_currency
  end
end

标签: ruby-on-railsruby-on-rails-5view-helpers

解决方案


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

  def cost_dollar
   number_to_currency(@book.cost)
  end

推荐阅读