Rails - Rails.root.join 部署后行为异常 [英] Rails - Rails.root.join misbehaving after deployment
问题描述
我的 app/assets 目录中有 javascripts 和 stylesheets 目录,就像任何普通的 Rails 应用程序一样.
I have, in my app/assets directory, the directories javascripts and stylesheets, as any normal Rails app.
不过,我也想有一个插件目录.
I wanted, however, to have a plugins directory as well.
示例:
app/assets/plugins/myPlugin1/somefile.js
app/assets/plugins/myPlugin1/somefile.css
事情是,如果我使用
<%= javascript_include_tag 'plugins/myPlugin1/somefile.js' %>
我会收到 404 错误,因为
I would get a 404 error, as
/assets/javascripts/plugins/myPlugin1/somefile.js
不存在.然后,我注意到插件"目录正试图在 javascripts 中访问.我认为这与javascript_include_tag"有关.
does not exist. I noticed, then, that "plugins" dir was trying to be accessed inside javascripts. I assume this have something to do with "javascript_include_tag".
经过一番研究,我意识到我必须将此行包含在 config/application.rb 中
After some research, I realized I had to include this line into config/application.rb
config.assets.paths << Rails.root.join("app", "assets")
它似乎奏效了.在 WEBRick 和开发方面,它运行良好.
And it seemed to work. On WEBRick, on development, it worked beautifully.
现在我成功部署到服务器,预编译资产,并认为我已经准备好了.
Now I deployed successfully to a server, precompiled the assets, and thought I was ready to go.
目录
/public/assets/
已创建.app/assets/javascripts"、app/assets/stylesheets"和app/assets/plugins"中的所有内容都被预编译为 public/assets
was created. Everything in "app/assets/javascripts", "app/assets/stylesheets" and "app/assets/plugins" were precompiled to public/assets
效果
app/assets/javascripts/login.js
能够在具有
<%= javascript_include_tag 'login.js' %>
什么不起作用
属于插件"的文件.
app/assets/plugins/myPlugin1/somefile.js
被预编译为
public/assets/myPlugin1/somefile.js
但是当我跑步时
<%= javascript_include_tag 'plugins/myPlugin1/somefile.js' %>
它试图访问
/javascripts/plugins/myPlugin1/somefile.js
根据我的开发经验,正确的路径应该是:
when the correct path, according to my experience in development, would be:
/myPlugin1/somefile-(some hex hash).js
<小时>
因此,如您所见,在生产中,我遇到了与未添加时相同的问题
So, as you can see, in production I'm experiencing the same issue as when I haven't added
config.assets.paths << Rails.root.join("app", "assets")
到 application.rb
to application.rb
可能是什么问题?
推荐答案
你可以这样:
Dir.glob("#{Rails.root}/app/assets/plugins/**/").each do |path|
config.assets.paths << path
end
或者设置每个插件,一个一个像:
Or set each plugin, one by one like:
config.assets.paths << Rails.root.join("app", "assets", "plugins", "myPlugin")
但是如果您在 myPlugin 目录上有子目录,则不会加载每个文件.因此,第一个选项可能更适合您.
But if you have subdirectories on that myPlugin directory would not load every file. So the first option might be better for you.
这篇关于Rails - Rails.root.join 部署后行为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!