I'm working on a Rails 4.0 app with following directory structure
-app
|->javascript
|->page-specific
|->myjavasript.js
In my application.css I have removed required tree directive to make sure I don't include all js on every page.
//= require_tree .
Now to include my javasript I used following syntax on my page:
The application works fine on my development environment but when I push my changes to my production environment, I get 404 when I browser make a call to get javascript.
I call made in production is:
server-name/javascripts/page_specific/myjavasript.js
I call made in develop machine is:
server-name/assets/page_specific/myjavasript.js?body=1
I read a bit about asset pipelines and added following to my production.rb:
config.assets.precompile += ['page_specific/myjavasript.js']
But still my public\assets folder doesn't contain myjavasript.js, and keep on getting a 404.
I have a couple of questions, I do I add page specific (not-cpntroller specific) assets to my app?
Why is it working on development and not in production, what is done differently?
How can I fix it?
In firebug I see:
"NetworkError: 404 Not Found - https://my-server/javascripts/page_specific/myjavasript.js"
Just try,
config.assets.precompile += ["page-specific/*"]
Then, open up rails console and run the below line to know whether page-specific folder is there under your app asset paths:
Rails.application.config.assets.paths
Hope it helps :)
Related
I have a .js file named:
app/assets/javascripts/generics/directory/edit_person.js
that isn't included in the application.js because there's not\ require_tree and looking at the network tab in the browser, I've confirmed the edit_person.js is being served by itself.
The erb being rendered is named:
app/views/company_area/directory/edit_person.html.erb
I don't see any script tags in here referencing edit_person.js, is there some magic that Rails is doing?
You'd have to show us the relevant code.
A Rails application typically will serve JS in one of three circumstances:
A JS file is specified in your html. This includes application.js in the layout file.
A JS request was made and you are responding to that JS. Example: the JS file edit_person.js exists in the views folder corresponding to the controller action edit_person and a client makes a .js request to that action
Webpacker is configured to handle JS alongside the asset pipeline. [1]
If none of these apply to you, then you will not see your JS loading on the page. Knowing that, you need to check your layout, application.js, and controller actions (responses).
[1] Webpacker would not likely be serving these files due to their location in your dirs, but it was worth noting for the sake of accuracy.
is this an app you wrote? If not I would check any of the layout files (particularly: views/layouts/application.html.erb or views/layouts/persons.html.erb if available )
I try to integrate angular to rails app. I extended assets folder by
config.assets.paths << Rails.root.join('node_modules')
and it's good work but my console has serveral messages error
http://localhost:3000/assets/rxjs/Subject.js.map
Rails app couldn't .map extension how to load it
Those are "javascript source map" files. If your Subject.js javascript file has been minimized/uglified, then you can generate and add a "map" file to let browsers know how to "unminimize"/"unuglify" your js file.
If the Subject.js file is written by you, then I guess your environment setup with different node.js and gulp.js modules has it enabled. Make sure those files are copied to your /assets folder as well.
Alternatively, you can disable them by removing special comment at the end of your javascript file:
//# sourceMappingURL=/assets/rxjs/Subject.js.map
Or, less likely, your server might be sending X-SourceMap header.
Noted Things
Rails 4.1.8
jquery rails 3.1.3
Windows 7
I am currently trying to run a simple coffescript under app/assets/javascripts/login.jscoffe
# login.js.coffee #
$(document).on "page:change", ->
$('#loginb').click ->
alert "Clicked!"
When I load up the page I get a console error.
Uncaught reference error $ is not defined
Now I have tried several things, such as using rake precompile. Made sure I included in application js below.
//= require jquery
//= require jquery_ujs
I've been reading up to see if there are other solutions. Some saying things about turbolinks. Should I just include the jquery right into the html page?
So I back upped a little I how I set up my first app. Using a windows 7 machine you run into a problem defined in this article ---> ExecJS::RuntimeError on Windows trying to follow rubytutorial
Since I took the easy way out(going into application.html.erb and changing the linking tag from "application" to "default" fixed a problem but also created more. I then had to load each script I wanted using the include tag and precompile assets files.
The solution came in when I took a step back and used the answer to the link above. Fixing the ExecJs and reintegrating the default functionally of the app seem to fix things.
I'm trying to add a js file which is part of a purchased theme to my rails project.
In my assets.rb file I have
Rails.application.config.assets.precompile += %w(mvpready-core.js)
In my application.js I have
// This is a manifest file that'll be compiled into application.
//= require mvpready-core
//= require_tree .
At the end of my user.html.erb I have
<%= javascript_include_tag "application" %>
But when I load the page console gives me the error
ReferenceError: mvpready_core is not defined
What am I doing wrong and how can I debug this?
I dont think you need to tell anything to the assets.rb file.
The proper way of integrating a purchased theme is to put the required assets files in the vendor folder of your rails project directory. The vendor folder is specially for the third party plugins such as bootstrap. So what you need to do is as follows:
STEP 1:
Copy and paste the necessary JS and CSS files into the particular folders of the assets inside vendor folder.
STEP 2:
Require those files in to the project's manifest file i.e application.js and application.css files.
You can find manifest files here:
app -> assets -> javascripts -> application.js and app -> assets -> stylesheets -> application.css
Let me help you understand a little bit more. Ruby on Rails has some magick that goes on in the background where you do not need to add anything to
assets.rb
once you have setup your new project all you need to do is put the javascript file into:
/app/assets/javascripts/mvpread-core.js
When you start the rails server it will autoload anything you have in following directories:
/app/assets/javascripts/mvpread-core.js
/app/assets/images/mvpread-core.png
/app/assets/stylesheets/mvpread-core.css
Now if the javascript has path's in it linking to images, other javascripts, and other stylesheets you will need to search through the source code and make sure that it is looking for the file in this url path structure:
/assets{javascripts|images|stylesheets}
Also as #Taylor Galeser asked did you put the file in /app/assets/javascripts ?
This is all a very overly simplistic explanation on what Ruby on Rails does automatically for you but it should help you get what is going on behind the scenes better.
I've migrated a rails 3.0 app to 3.1 on Heroku. It's running on the cedar stack and everything is fine except that the app's javascript won't run. The application.js file is compiled and looks just as it should. It's accessible by going to myapp.com/assets/application.js. It just doesn't get run!
If I run the app locally, the javascript works, so I suspect that there must be some simple configuration issue that I'm missing. Here's my production.rb file:
FloridaBirdTrail::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
# Compress JavaScripts and CSS
config.assets.compress = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
edit
Replacing the contents of production.rb with that of development.rb has allowed at least some of my javascript to run (gmap3 isn't working, for some reason). But which of the settings is making the difference?
Open your application.rb file and make sure your Bundler require statement looks like the following:
Bundler.require *Rails.groups(:assets)
By default it looks like
# If you precompile assets before deploying to production, use this line
Bundler.require *Rails.groups(:assets => %w(development test))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
Manually precompiling worked for me.
bundle exec rake assets:precompile before you push to heroku.
Were you sure to switch Heroku to the Cedar stack? Here are some docs for upgrading
http://devcenter.heroku.com/articles/rails31_heroku_cedar#upgrading_from_previous_rails_31_releases
http://devcenter.heroku.com/articles/cedar
I had some Javascript problems (server-side on Heroku, locally everything was fine) that went away when I
* moved all my Javascript into a separate file instead of application.js
* removed require_tree from application.js and instead called up every javascript I wanted by name
* removed bootstrap.js from my app/assets/javascript folder
My guess is that compilation somehow screws things up.
I hope you did a local pre-compilation of assets before your latest Heroku push (as advised in one of the responses above).
Please check if your system is blocking the execution of JavaScripts. For this, open up the console while you are on your Heroku app, and check for exceptions. In case you see exceptions related to JavaScripts being blocked, that could be the issue. In my case, the same happened, and unfortunately, I was not able to do anything about it, as I didn't have admin privileges.