in my app/assets/javascripts/specific.js I have
//= require_tree ./specific
in app/assets/javascripts/specific/chat I have pusher.js
Also, in config/environments/production I have
config.assets.precompile += %w( specific.js some_other_manifest.js )
However, when I go into production (on heroku) it still complains pusher.js is not precompiled. What am I doing wrong here?
While the application is deployed, the javascript files seem to be compiled.
from Heroku logs:
Compiled specific/chat/pusher.js (0ms) (pid 1042)
Compiled specific.js (60ms) (pid 1042)
But when I go to the view,
ActionView::Template::Error (specific/chat/pusher.js isn't precompiled):
1: <%= javascript_include_tag 'specific/chat/pusher' %>
2: <%= javascript_include_tag params[:controller] %>
3:
4: <div id="chat-header">
app/views/messages/index.html.erb:1:in `_app_views_messages_index_html_erb___3285714722884343394_70246542189040'
I also tried putting config.assets.precompile .. option inside config/application.rb instead of production.rb
Related: Assets say "not precompiled" when they are precompiled
When I run assets:precompile and look in public/assets folder, I see that they are all precompiled (e.g. specific-bfgbfbf4534535.js)
So the asset is actually precompiled, but the error says it isn't precompiled
From my view:
<%= javascript_include_tag 'specific/chat/pusher.js' %>
You don't include specific files, you include the entire manifest, that's the whole point of manifests.
This can't work:
<%= javascript_include_tag 'specific/chat/pusher' %>
Instead, you need a single include for the top-level manifest:
<%= javascript_include_tag 'specifics' %>
From your comment below your question:
It's not supposed to be included in application.html.erb
That isn't how precompiled assets work. You need to include specifics.js, or build another (more granular) manifest. The point of manifests is that they produce a single minified blob of code to be included. You specifically say you see a specific-bfgbfbf4534535.js in your compiled assets folder; that is the file that will be included, and it contains pusher.js.
try to precompile all assets:
config.assets.precompile += %w( *.css *.js )
Related
I am working with a Rails 5.2 application
Locally I added a new image to app/assets/images/my-file-name.jpg
and then in my .erb file I reference it using
<%= image_tag "my-file-name.jpg" %>
Then when I deploy to Heroku I run the following steps locally
rake assets:clobber <!-- this destroys the public/assets folder as expected
RAILS_ENV=production bundle exec rake assets:precompile this creates a new public/assets folder but now each of the files within the assets folder have a hash after their name. So my file my-file-name.jpg has become my-file-name-{BigLongCacheBustingHashHere}.jpg
git push staging <-- Deploy to my staging environment and check that the log says assets compiling -- all good.
Check staging environment if the image is being served and I notice that the app cannot find my image because my <%= image_tag my-file-name.jpg %> is still producing a static html link WITHOUT the hash. It looks like this <img src="my-file-name.jpg"> when I believe it should be producing a tag that looks like <img src="my-file-name-{BigLongCashBustingHashHere}.jpg"
Why do you think the <%= image_tag %> is not producing production friendly urls?
I can see that it is working elsewhere in the app so not sure where I am going wrong.
OMG I lost hours of my life on this. Rails assets does not like .jpeg extensions and will flat out ignore them in production. So I just changed the file name to my-file-name.jpg and now Rails is happy
We have a Rails 5 application and recently updated Sprockets from the 3.x series to 4.0.2. Now Rails can't find an asset (CSS file) belonging to a vendored gem. Note that the gem is an engine and the asset is called from a template inside the engine.
The asset is referenced inside a file at vendor/gems/our_vendored_gem/app/views/layouts/ like this:
<%= stylesheet_link_tag "our_vendored_gem/application", :media => "all" %>
The file is at vendor/gems/our_vendored_gem/app/assets/stylesheets/our_vendored_gem/application.css
Then we get the error Sprockets::Rails::Helper::AssetNotPrecompiled in OurVendoredGem::Mocks#index.
Our app/assets/config/manifest.js looks like this:
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
I tried adding
//= link_tree ../../../vendor/gems/our_vendored_gem/app/assets/stylesheets/ .css
...but that did not help. The vendored gem has a manifest file of its own but it is unclear to me if Sprockets is already reading it, or if it needs to be called somewhere.
What needs to happen to make this file available to Rails again?
After some digging, I found this issue comment which explains that a Rails engine should have its own manifest file; that file can then be included in the application manifest like this:
//= link my_engine
This will find a manifest file at my_engine/app/assets/config/my_engine.js and add those directives to those in the application manifest.
I am new to Rails 5.0.0.1
I followed the guidelines in http://guides.rubyonrails.org/asset_pipeline.html#controller-specific-assets:
You can also opt to include controller specific stylesheets and JavaScript files only in their respective controllers using the following:
<%= javascript_include_tag params[:controller] %> or <%= stylesheet_link_tag params[:controller] %>
When doing this, ensure you are not using the require_tree directive, as that will result in your assets being included more than once.
I generated some scaffolds (which also generated the assets -- .coffee and .scss)
When I do rails s and access one of the scaffold's main page, it generated an error:
Asset was not declared to be precompiled in production.
Add Rails.application.config.assets.precompile += %w( myscaffold.css ) to config/initializers/assets.rb and restart your server
I do as it said, and it worked, but is there any other way to simplify this process?
I wish that I can generate some other scaffolds or controllers with their assets without doing any more concern of adding new lines inside the config/initializers/assets.rb.
I will receive any other alternatives too.
you just try this..
check your Gemfile gem 'sprockets-rails', '2.3.3'
And bundle install
Navigate files
config/initializers/assets.rb
Then add
Rails.application.config.assets.precompile += %w( your_file_name.scss )
Restart server
I'm migrating an app to Rails 3. The following - which I've seen recommended in a few places - does not work:
<%= javascript_include_tag :defaults %>
In my case, it expands to this:
<script src="/assets/defaults.js" type="text/javascript"></script>
... which results in a 404. As I understand it, the :defaults is not supposed to include a file called "defaults.js"; it's supposed to include a few essential things like prototype.js and application.js.
Note that in my case, the following works fine. It's just that I'd rather use the official recommended way, if possible:
<%= javascript_include_tag :prototype %>
<%= javascript_include_tag :application %>
I'm running Rails 3.2.8 with ruby 1.9.3.
I do not have the following line in my config/application.rb (in any form). To migrate to Rails 3, I created a new Rails 3 application, and used that application.rb as a starting point:
config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js)
In app/assets/javascripts, I have:
Util.js
application.js
controls.js
dragdrop.js
effects.js
prototype.js
... and a bunch of stuff specific to my application.
Since Rails 3.1, it uses assets pipeline. It means you need to change your management of assets.
You have a assets/javascripts/application.rb file which contains something like this:
//= require jquery
//= require jquery_ujs
//= require_tree .
It seems you include jquery, jquery_ujs and all other files in the javascripts repository. With the last line you don't need to do anything else in this file. You just need to include the application file in your view and rails will manage everything:
<%= javascript_include_tag "application" %>
It's exactly the same thing with stylesheets.
Then in production environment, assets (images, stylesheets, javascripts) will be compiled and minified in order to be more efficient.
I suggest you read more about here
Hope this helps
Here's a lot more about the Asset Pipeline - http://guides.rubyonrails.org/asset_pipeline.html
It's meant to make it easier to break up your Javascript into separate files for development, yet compile and minimize them in production.
I'm using Rails 3.1. My JavaScript and CSS files are included in usual way:
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
Now I want to create standalone version of existing page with default layout. I want it to be a single html file that user can download and use in offline. So, JavaScript and CSS code must be included in content of this file. How can I do this?
You can create a separate CSS and JS and include them in config.assets.precompile key in the file config/application.rb.
Check this link