"Chartkick is not defined" error, using rails - javascript

I'm trying to make some simple charts in rails 6, using the chartkick gem. I strictly followed the quickstart on chartkick.com, but I can't get the graphs to load. I keep getting the error "Uncaught ReferenceError: Chartkick is not defined" in my browser.
I installed the gem.
I ran yarn add chartkick chart.js
This is my complete application.js file
require("turbolinks").start()
require("#rails/activestorage").start()
require("channels")
require("chartkick")
require("chart.js")
In app/views/layouts/application.html.erb I have the line
<%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" %>
The build itself runs fine, but the js is giving me errors in my browser. Where is this coming from?
I know similar questions have been asked here, but none of them can get me any further.

Apparently the problem was coming from something else entirely. My controller inherited from ActionController::Base instead of ApplicationController, so the javascript packs didn't load from my application.html.erb.
Since I've corrected it, everything works fine now.

Related

javascript asset dosen't load in my html.erb page

I am using :
Linux Ubuntu 16.04
ruby 2.4.0p0;
rails 5.1.1
I am a begginner in rails and I have followed the Ruby on Rails 4 Essential Training in lynda.com in order to learn. I am now stuck at the loading js assets task.
I have created the "app/assets/javascripts/demo.js" file which contains the js function :
function jsRoar(name) {
alert('I am ' + name + '. Hear me roar!');
}
Then I have load this file in my manifest file "app/assets/javascripts/public.js"
//= require jquery
//= require jquery_ujs
//= require demo
After that I have added external include of js to my layout at "app/views/layouts/application.html.erb":
<%= javascript_include_tag "public" %>
In my controller I have included my layout by typing at "app/controllers/demo_controller.rb"
layout 'application'
And finally I have called my function by typing these js codes in my demo/index.html.erb :
<%= link_to('Roar', '#', :onclick => "jsRoar('Yassser');return true;") %>
<%= javascript_tag("jsRoar('Rails');")%>
After that i get "undefined function" in my chrome console and the alert doesn't appears .I have defined the function in the same html.erb and it works so it's not a problem of javascript i think.
I must mention also that, at first when i run my server, i have obtained a problem of assets compilation so I have fixed by adding this line in "config/initializers/assets.rb":
Rails.application.config.assets.precompile += %w( public.js)
I am available for more details.
Please help.
My problem was resolved after deleting the "demo.coffee" file.
I discovered that the my issue is derived from the fact that I have two files with the same name (but different extensions) "demo.js" and "demo.coffee". Since the Rails asset helpers treat coffescript and js files equally this is ambiguous.
So I have deleted the "demo.coffee" and all my problems are settled.
Thank you all mates.
Great thnx for #max with his reply in this post
I think this:
<%= javascript_tag("jsRoar('Rails');")%>
should be:
<%= javascript_include_tag("demo") %>
See if this code works in your view:
$(document).ready(function(){
jsRoar('Yassser');
})

Rails: $ is not defined

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.

Precompile json data and include with Ruby on Rails

I am trying to precompile and use Bootstrap Toggle with Ruby on Rails on Heroku.
Here is the JavaScript console error I am getting when I compile the bootstrap-toggle.min.js.map file that also comes with the css and js files,
Uncaught SyntaxError: Unexpected token :
application-4df1877ce16cb310440b73a84d39fd58.js:14555
And the error line is a long json object that basically looks like,
{"version":3,"file":"bootstrap-toggle.min.js", ... }
Why am I getting this JavaScript error?
And also, should I have this line in my application.js file?
//= require bootstrap-toggle.min.js.map
UPDATE:
If someone does want to look into why I can't precompile json with getting JavaScript console errors that is great. I found a work around by not precompiling the json at all at including it in my public/assets folder. I updated the linked to that folder.

Rails application not pre-compliling asset into public\assets

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 :)

What can cause Rails 3.1 "= require jquery" to stop working?

I'm porting a Rails 3.0.9 app to Rails 3.1.rc5. My application.js is exactly the same as one generated by Rails 3.1 itself:
// This is a manifest file ...
//
//= require jquery
//= require jquery_ujs
//= require_tree .
But when I run my app and look at the application.js in Firebug or Chrome Developer Tools, all I see is:
// This is a manifest file ...
//
The directives are gone, so it would seem that the file has been processed by Sprockets, but the directives have not been replaced by the contents of jquery et al. There are no errors appearing on the server console or in the logs.
Curiously, when I run a blog app (you know, the canonical tutorial app) it works fine (that is, when I examine application.js in Firebug, it contains the text of jQuery.) This would seem to indicate that something in my app is somehow interfering with Sprockets. Has anyone out there heard of such an issue (and hopefully a workaround)?
Here's my setup:
$ gem list jquery
*** LOCAL GEMS ***
jquery-rails (1.0.12)
$ ruby -v
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
$ rails -v
Rails 3.1.0.rc5
I'm at a loss as to what might be wrong. I've triple checked my Gemfile; I've run and re-run bundle install and bundle update; I've tried rc3, rc4 and now rc5; I'm running Ruby 1.9.2p290. Any ideas?
One workaround: include JavaScript files with the old-skool tag. For example, in my (Haml) layout:
= javascript_include_tag '/assets/jquery.js'
= javascript_include_tag '/assets/jquery_ujs.js'
= javascript_tag 'jQuery.noConflict();'
The /assets/ prefix tells Rails 3.1.x to look on the asset path, which includes gems, so you'll get the same files as with Sprockets directives. But you won't get concatenation or any other Sprockets preprocessing.
Still looking for better solutions.

Categories

Resources