Rails - Rails.root.join misbehaving after deployment

1.7k Views Asked by At

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

There are 1 best solutions below

4
On BEST ANSWER

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.