Rails - Rails.root.join misbehaving after deployment
Asked Answered
W

1

2

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?

Waylay answered 25/12, 2015 at 21:56 Comment(0)
J
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.

Jameejamel answered 26/12, 2015 at 4:27 Comment(4)
Tried this for Dir.glob("#{Rails.root}/app/assets/dashboard**/").each do |path| but couldn't see the folder assets in the console with Rails.application.config.assets.pathsTengler
are you missing a '/' before those two * or maybe the two * should be after the last slashJameejamel
Nope, I've tried Dir.glob("#{Rails.root}/app/assets/dashboard/**/").each do |path| as well as Dir.glob("#{Rails.root}/app/assets/**/*/").each do |path| and a few other combinationsTengler
That's odd, it worked for me. Try to debug, restart stuff, check spelling or something, I can't help you more than thatJameejamel

© 2022 - 2024 — McMap. All rights reserved.