Rails assets not available on production - javascript

I have this specific issue. On a (LARGE) rails setup I have a backbone project in /app/assets/reader/. All of my javascript assets are precompiling dynamically into reader.js, this works fine. My i10n files in locale/ don't play nice however because they don't need any precompiling. In development it works fine, but in production they are not available.
In my /app/views/layouts/reader.html.erb file I have the following lines:
<%= javascript_include_tag "reader" %>
<%= javascript_include_tag "locale/en" %>
The problem is that the lower one results in a 404 error on production.
I've tried the following alternatives:
<%= javascript_include_tag "en" %>
<%= javascript_include_tag "locale/en" %>
<%= javascript_include_tag "reader/locale/en" %>
None of these seem work. The last one even broke on development.
PS: in applicaton.rb I have:
...
config.assets.precompile += [
...
'reader.js',
...
]
...
config.assets.paths << File.join(Rails.root, 'app', 'assets', 'reader', 'locale')

You removed all files in public directory folder then restarted server? Also inside of your js folder you have a folder
reader/locale/en.js
? Double check all paths and if it works in development you should check out your error log in production to see where the problem is coming from.

The solution was not in the javascript_include_tag, but rather in the way config.assets.precompile was formatted.
By default Rails scans for any subfolder DIRECTLY within assets. That meant that the locale file had to be added as locale/en.js to config.assets.precompile, and that the config.assets.paths line was not even necessary at all.
The way rails scans subfolder is really specific and important. Get that right, and it should all work like a breeze. Once you know how it works, it actually gets quite powerful.
Pro tip:
I ended up using locale/*.js in combination with <%= javascript_include_tag "locale/#{I18n.locale}" %>, as I actually have a lot of locale files.

Had the same issue, turns out there is a line in config/environments/production.rb:
config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?
which didn't allow Rails to serve static files, including the precompiled assets. Changing it to
config.public_file_server.enabled = true
fixes the issue.
This is caused by the fact that Nginx and Apache handle static serving on their own. But Rails is configured to use Puma by default so this doesn't really make too much sense for it to default to these settings.

Related

Ruby on rails javascript doesn't load properly (HTML5 template)

I am very new in ruby, so please don't judge me too much :)
I was trying to make this template http://freebiesbug.com/code-stuff/sedna-one-page-website-template/ run on rails. And it went pretty well.
However, I have stuck on configuring javascript parameters (the way it loads on rails). I am not sure how to do that correctly. Things that slides in the template doesn't work for me, like apple images and slider of employees in the bottom...
I tried to include all file names in javascript.rb file and also I have used:
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
but non of these worked.
Any ideas where I may made a gap?
Thanks!
Javascript files end in .js, not .rb, so thats one problem.
The line
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
tells rails to package and include the application.js file found at app/assets/javascripts/application.js. Assuming your template has one associated javascript file, you can copy and paste the contents of that file into your application.js file (which may be the easiest solution). You can also add any javascript (or coffeescript) files to the app/assets/javascripts folder, and those files will be packaged and served up in the default configuration of rails.

Amazon cloudfront giving different assets for different user?

I am having following folder structure in rails app.
|-assets
|-javascript
|-lib
bootstrap.js
jquery.js
application.js
|-styleesheet
|-lib
bootstrap.js
application.js
the static assets are hosted in cloudfront by diffreent people I don't know how it was done.
I have given the following config in production.rb.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.compile = true
config.action_controller.asset_host = 'cloudfront url'
In view Page it will be like this:
<%[application,lib/bootstrap].each do |css_url| %>
<%= stylesheet_link_tag css_url%>
<%end%>
<%[application,lib/bootstrap,lib/jquery].each do |js_url| %>
<%= javascript_include_tag js_url%>
<%end%>
When deployed in production and for the first request it will compile assets folder and store them in cache. for subsequent request it will take from cache.But every time I host and make first request cache generated for only some files, not for all the files.
Your question is not very clear but please ensure that both users are receiving the same fingerprints in the files. If the fingerprints are not the same that it is likely an error not with cloudfront but something is wrong with your rails application.
The most likely cause for this error is that some multiple application workers are running (some serving older assets and some newer).
If the above does not help you solve the problem, please update with the specifics of your rails setup - caching layer, load balancer, application server etc.

Rails: couldn't find file 'typeahead.js'.. until i remove 'data-turbolinks'

I installed Elasticsearch (with searchkick gem) following this tutorial - https://shellycloud.com/blog/2013/10/adding-search-and-autocomplete-to-a-rails-app-with-elasticsearch.
Search is working and now I'm implementing autocomplete function with typeahead.js which I installed using Bower.
However I get the error message:
Sprockets::FileNotFound at /books couldn't find file 'typeahead.js'
I tried to include it in various orders in my assets file, without luck..
However I realized removing turbolinks from application.html.erb solved the problem.
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
This does not seem right... How can I resolve this?
I had this exact same problem. The author named the library with the .js extension added and I assume it has something to do with application.js ignoring the .js part at the end. I had this problem when using rails-assets and once I poked around more in the gem source I saw two .js.js extensions added.
You can add it as so:
//= require typeahead.js.js
Then you can re-enable
'data-turbolinks-track' => true

Asset filtered out and will not be served [duplicate]

This question already has answers here:
Asset filtered out and will not be served: add `config.assets.precompile
(5 answers)
Closed 8 years ago.
Asset filtered out and will not be served: add Rails.application.config.assets.precompile += %w( login.js ) to config/initializers/assets.rb and restart your server
I ge the above error when i try to run my application.
<% content_for :javascripts do %>
<%= javascript_include_tag 'login' %>
<% end %>
I have placed all my js files in assets/javascripts, but still i get the above error.
As an alternative solution, you can also comment out this line on development.rb:
config.assets.raise_runtime_errors
or set it to false
This will disable sprockets runtime errors (the "Asset filtered out and will not be served" yada yada error) in development.
Keep in mind that by doing this, you can mask production errors for assets: eg. some assets will be served in development but not in production. (see this)
Use it if you know what you're doing :)
hth
You must include all js/css files that you include with #javascript_include_tag and #stylesheet_link_tag methods in Rails.application.config.assets.precompile array.
Check out config/initializers/assets.rb (create if it does not exist). That's how it should look:
Rails.application.config.assets.precompile += %w( login.js )
Restart Rails server after editing.
By the way it is self-evident from error's text.
Check out Rails Assets Pipeline documentation for details: http://guides.rubyonrails.org/asset_pipeline.html .
I am doing something similar, but I've added my additional assets in Production.rb
config.assets.precompile += ['landing.css', 'landing.js']

Where should javascript files and stylesheets be added to a Rails project?

I'm unclear about where javascript files and stylesheets should be added to a Rails project. Perhaps they should be added to public/javascripts and public/stylesheets respectively. Then they're copied to app/assets? Or perhaps it's the other way around.
I've experimented with <%= stylesheet_link_tag :all %> and :default and just explicitly naming my stylesheets but I'm still confused.
Where should javascript files and stylesheets be added to a Rails project?
It is the other way around. You add stylesheets and javascripts to their respectable folders in assets.
You then use:
= stylesheet_link_tag "application"
= javascript_include_tag "application"
Check the guide for details http://guides.rubyonrails.org/asset_pipeline.html
In a Rails 3.1 application, where the asset pipeline is enabled, you want to place your script/stylesheet assets in app/assets. Rails will automagically find them via Sprockets and serve them up minimized and contaminated (in production).

Categories