Ralis模块和调用静态方法

时间:2019-11-21 22:13:40

标签: ruby-on-rails module static-methods

我正在尝试在Module中调用静态方法。

module CoursesHelper

    include ActionView::Helpers::NumberHelper

    def convert(old_price)
        daily_currency = Rails.cache.fetch('daily_currency', expires_in: 12.hours) do
            CurrencyConverter.get_value # <- static. 
        end
        new_price = daily_currency * old_price
        number_to_currency(new_price.round(-2))
    end

end

然后我在Rails项目中上了课。

class CurrencyConverter
    def self.get_value # <- declared as static
        response = RestClient::Request.execute(
            method: :get,
            url: 'https://api.someapicall........'
        )
        value = JSON.parse(response)["rates"]["etc"]
        value
    end
end

我收到了这个错误

uninitialized constant CoursesHelper::CurrencyConverter

这是为什么? 如果这不是最佳做法,您会以Rails的方式让我知道吗?

编辑

文件夹结构

├── helpers
│   ├── application_helper.rb
│   ├── courses_helper.rb
│   ├── currecy_converter.rb
│   ├── devise_helper.rb
│   ├── introduction_helper.rb
│   ├── orders_helper.rb
│   ├── posts_helper.rb

1 个答案:

答案 0 :(得分:0)

文件File的拼写错误。是错误的。对不起大家。

currecy_converter.rb

应该

currency_converter.rb

更改后。工作正常。文件名在Rails中也很重要吗? :(

相关问题