Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not finding my asset #17

Open
kingdonb opened this issue Mar 21, 2017 · 5 comments
Open

Not finding my asset #17

kingdonb opened this issue Mar 21, 2017 · 5 comments

Comments

@kingdonb
Copy link

I am using a custom RAILS_ENV=staging for my QA environment, but I think this issue still exists if I set RAILS_ENV=production. I looked through your code for specific references to environment names and found that they had been removed, so I'm guessing this should work. (Maybe it's something addressed by the open pull request. Not sure.)

I have a little red "X" png that is part of my image assets, and it needs to be included in some javascript code that adds nested attributes as part of a workflow for file attachments. The file x_16x16.png is in the asset pipeline.

$ RAILS_ENV=staging rails c
Loading staging environment (Rails 4.2.7.1)
2.3.0 :001 > ActionController::Base.helpers.asset_path('x_16x16.png')
 => "/assets/x_16x16-7d1b5578d7288518ff6184d04063cf116faf9ab5a52d7f19898af57280de5303.png"

However, I include this snippet:

content += "<td><a href='javascript: void(0);' onClick='remove_attachment(this," + i + ");'><img src='" + asset_path('x_16x16.png') + "'></a></td></tr>";

and it comes out like:

<a href="javascript: void(0);" onclick="remove_attachment(this,0);"><img src="/assets/x_16x16.png"></a>

Can you help? I have found no other way to include asset paths in javascript cleanly.

@kingdonb
Copy link
Author

It doesn't seem to work either, if I changed the file to .js.erb and use <%= asset_path('x_16x16.png') %> or <%= image_path('x_16x16.png') %>

Possible that I'm doing something else wrong...

@kingdonb
Copy link
Author

Actually I had this line of code repeated, and I was changing the wrong one.

I still get <img src="undefined"> with the js_assets gem way, which is not right, but I can use .js.erb and <%= asset_path(...) %> to get the asset path.

Rails version 4.2.7.1 FYI

@kingdonb
Copy link
Author

I needed this (to poke around a bit, and read README more thoroughly, it was in there)

JsAssets::List.allow << '*.png'

Now I made some progress,

$ RAILS_ENV=staging rails c
Loading staging environment (Rails 4.2.7.1)
2.3.0 :001 > JsAssets::List.fetch["x_16x16.png"]
 => "/assets/x_16x16-7d1b5578d7288518ff6184d04063cf116faf9ab5a52d7f19898af57280de5303.png"

but I still have something blocking it from populating window.project_assets, as long as I run with my RAILS_ENV=staging variable set. It actually works fine if I do rake assets:precompile with no RAILS_ENV set. I get a different asset compiled and it shows up in the dictionary window.project_assets, where it can be found by the global js function asset_path.

In other words, it's workable for me at this time, but I still think something is wrong. I think it should also work with RAILS_ENV=staging rake assets:precompile. I'm afraid my capistrano environment does set this variable, and I'm going to hit this bug in my QA environment (staging) unless I find a permanent workaround. I'll keep tracing until I find what causes the problem and follow up.

I think for sure my asset pipeline is implemented correctly and this has to be a quirk in js_assets. Maybe it is something weird about precompiled assets that I just don't understand.

@kingdonb
Copy link
Author

kingdonb commented Mar 21, 2017

I'm at a loss.

window.project_assets = <%= JsAssets::List.fetch.to_json %>;
window.asset_path = function(logical_path) {
  return window.project_assets[logical_path];
};

and

$ RAILS_ENV=staging rails c
Loading staging environment (Rails 4.2.7.1)
2.3.0 :001 > JsAssets::List.fetch["x_16x16.png"]
 => "/assets/x_16x16-7d1b5578d7288518ff6184d04063cf116faf9ab5a52d7f19898af57280de5303.png"

but

$ find public/assets/ -type f -exec rm {} \;
$ RAILS_ENV=staging bundle exec rake assets:clean assets:precompile
...

$ grep window.project_assets public/assets/application-*.js
window.project_assets = {};
  return window.project_assets[logical_path];

@kingdonb
Copy link
Author

Remember to delete tmp/cache, kids. This was also in the README.

Works now. Thanks! Great gem. I will leave this open but you can close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant