Rails - Rails.root.join misbehaving after deployment

孤街浪徒 提交于 2019-12-01 09:15:07

问题


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.

Example:

app/assets/plugins/myPlugin1/somefile.js
app/assets/plugins/myPlugin1/somefile.css

Thing is, if I use

<%= javascript_include_tag 'plugins/myPlugin1/somefile.js' %>

I would get a 404 error, as

/assets/javascripts/plugins/myPlugin1/somefile.js

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".

After some research, I realized I had to include this line into config/application.rb

config.assets.paths << Rails.root.join("app", "assets")

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.

the directory

/public/assets/

was created. Everything in "app/assets/javascripts", "app/assets/stylesheets" and "app/assets/plugins" were precompiled to public/assets

WHAT WORKED

app/assets/javascripts/login.js

was able to be accessed in a view that had

<%= javascript_include_tag 'login.js' %>

WHAT DIDN'T WORK

Files that belonged to "plugins".

app/assets/plugins/myPlugin1/somefile.js

was precompiled to

public/assets/myPlugin1/somefile.js

But when I ran

<%= javascript_include_tag 'plugins/myPlugin1/somefile.js' %>

it tries to access

/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")

to application.rb

What could be the problem?


回答1:


You could go this way:

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")

But if you have subdirectories on that myPlugin directory would not load every file. So the first option might be better for you.



来源:https://stackoverflow.com/questions/34466781/rails-rails-root-join-misbehaving-after-deployment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!