Rails:添加的资产路径的类型为Pathname而不是String

时间:2018-11-01 00:35:41

标签: ruby-on-rails ruby npm node-modules

我已将node_nodules添加到我的rails资产路径中:

Rails.application.config.assets.paths << Rails.root.join('node_modules')

当我使用以下方法查看Rails资产路径时:

Rails.application.config.assets.path

我得到以下输出:

["/Users/gerardsimpson/pier/ruby/ies-portal/app/assets/config",
 "/Users/gerardsimpson/pier/ruby/ies-portal/app/assets/images",
 "/Users/gerardsimpson/pier/ruby/ies-portal/app/assets/javascripts",
 "/Users/gerardsimpson/pier/ruby/ies-portal/app/assets/stylesheets",
 "/Users/gerardsimpson/.rvm/gems/ruby-2.4.0/gems/coffee-rails-4.2.2/lib/assets/javascripts",
 "/Users/gerardsimpson/.rvm/gems/ruby-2.4.0/gems/actioncable-5.2.1/lib/assets/compiled",
 "/Users/gerardsimpson/.rvm/gems/ruby-2.4.0/gems/activestorage-5.2.1/app/assets/javascripts",
 "/Users/gerardsimpson/.rvm/gems/ruby-2.4.0/gems/actionview-5.2.1/lib/assets/compiled",
 "/Users/gerardsimpson/.rvm/gems/ruby-2.4.0/gems/turbolinks-source-5.2.0/lib/assets/javascripts",
 #<Pathname:/Users/gerardsimpson/pier/ruby/ies-portal/node_modules>,
 "/Users/gerardsimpson/.rvm/gems/ruby-2.4.0/gems/react-rails-2.4.7/lib/assets/react-source/development",
 "/Users/gerardsimpson/.rvm/gems/ruby-2.4.0/gems/react-rails-2.4.7/lib/assets/javascripts/"]

为什么我添加的路径类型为Pathname而不是类型为String?这会对我的申请产生意想不到的后果吗?

1 个答案:

答案 0 :(得分:0)

  

为什么我添加的路径名为Pathname而不是String类型?

因为在Rails 3和更高版本的Rails.root中返回了Pathname类型的对象。调用该对象的join方法将返回另一个Pathname

如果您想要一个字符串,则必须添加.to_s

Rails.root.join('node_modules').to_s
  

这会对我的申请产生任何意想不到的后果吗?

我不这么认为。加载程序搜索文件时,路径会转换为字符串。