Chartkick on Heroku ReferenceError: Can't find variable: Chartkick? - ruby-on-rails

I'm using chart kick in a standard way, like how the GitHub instructions dictate and it works in a local version. When uploaded to Heroku my charts produce an error "ReferenceError: Can't find variable: Chartkick" on the rendered partial page.
this is what I've done so far.
gemfile:
gem 'chartkick'
application.js:
//= require Chart.bundle
//= require chartkick
//= require_tree .
my show view renders a partial that calls a controller action via path method.
my controller action:
def event_names
#applet = RegisteredApplication.find(params[:id])
render json: #applet.events.group(:name).count
end
my partial that calls the action:
<%= pie_chart event_names_registered_application_path %>
I'm thinking the reason I'm getting the "ReferenceError: Can't find variable: Chartkick?" is because of the asset pipeline. But I don't fully understand what i can do to solve it is. please help :D

Just ran into the same issue. Make sure that application.js gets loaded before the Chart in your view. See details

Related

Select2-rails is not working with ActiveAdmin

I have difficulties integrating select2-rails with ActiveAdmin. I followed setup steps on
Select2-rails Github page: https://github.com/argerim/select2-rails and I added line:
//= require select2
to app/assets/javascripts/application.js and line:
*= require select2
to app/assets/stylesheets/application.css
so I assume when I have page in ActiveAdmin I should be able to add line:
$('#add_student_select').select2()
to active_admin.js.coffee
But its not working. In console I can see following error:
Uncaught TypeError: undefined is not a function
(anonymous function)
fire
self.fireWith
jQuery.extend.ready
completed
I also followed this StackOverflow question which recommends to add this line to active_admin.css.scss:
body.active_admin {
#import "select2";
}
But then I get following error:
File to import not found or unreadable: select2.
Do I integrate it correctly? I don't think that ActiveAdmin is able to get even access to the librabry.
If you're adding Select2 to the ActiveAdmin interface, you must add the javascript and styles to the ActiveAdmin assets:
# app/assets/javascripts/active_admin.js.coffee
#
#= require select2
#
# ...
And the stylesheets:
// app/assets/stylesheets/active_admin.css.scss
//
//= require select2
//
// ...
In the example you provided, Select2 would be available to the main Rails application, but not ActiveAdmin. ActiveAdmin uses its own javascript and stylesheet files.
I had met the same issue making select2 work with activeadmin, but instead I used a gem named activeadmin-select2. I had installed it according to the README, but I still got error "File to import not found or unreadable: select2.". It seems like that select2-rails had not been installed or not be accessible, however. So then I tried to add select2-rails to my Gemfile, and bundle, lastly, everything went well. You should checked your gem loading before you can make it work.

Argument 'PostCtrl' is not a function, got undefined

I've looked at about 10 answers to similar questions, and still can't figure this one out.
I'm trying to add some Angular to a preexisting Rails app I have, and started by following these tips.
So I have this in my app:
application.coffee
#= require jquery
#= require jquery_ujs
#= require bootstrap
#= require libraries/angular.min
#= require libraries/angular-resource.min
#= require_tree .
#= require main-ng
then this
main-ng.coffee
#= require_tree ./controllers
angular.module('Posts', [])
#MainApp = angular.module("MainApp", [
# modules the app depends on -- this is all I've got so far.
"Posts"
])
In layouts/application.html.haml this is all I have relating to angular:
%html{ 'ng-app' => "MainApp" }
For some reason, doing ng_app: 'MainApp' didn't work here, though that syntax seems to work elsewhere (and has in other established apps with Angular I've coded in).
Then there's just the view with the controller:
posts/show.html.haml
.row.container{ ng_controller: 'PostCtrl' }
# Using hashrocket syntax brings up the same error, while misspelling it does nothing,
# so this syntax appears to be working here
and finally, the controller:
controllers/PostCtrl.coffee
#= require directives/markdown
postModule = angular.module("Posts", ["markdown"])
console.log postModule # returns angular object on page load
ctrl = postModule.controller "PostCtrl", ($scope, $http, $location) ->
$s = $scope
console.log 'Inside controller' # doesn't get hit
console.log ctrl # returns angular object on page load
Any ideas what noobish error I'm committing here? I've hit a wall here.
Possible clue -- changing the name of the controller in the view changes the name in the error, but none of the console logs, so it basically seems to be an error finding that particular controller.
Another thing of note -- requiring the js controllers directory in my main-ng.coffee file and declaring the controller on the MainApp module instead of the Posts module both don't seem to change anything.
I see your Post module declared twice
angular.module('Posts', [])
and
postModule = angular.module("Posts", ["markdown"])
I suggest copy the second one and replace first. Then remove the second module.
Both these syntax create new module so the one added earlier is overridden.

Rails 4.0. vendor/assets/javascripts - Asset Pipeline issue

I am running Rails 4.0.1 and Ruby 2.0.0. I currently have a graph.js that takes inputs in from the user for a savings calculator in order to create a graph with d3 and the rickshaw.js graph.
My graph.js file is saved in the app/assets/javascripts/graph.js. I make a call to the Rickshaw graph with
var graph = new Rickshaw.Graph()
I am getting an error of Uncaught ReferenceError: Rickshaw is not defined.
The rickshaw.js file is saved in vendor/javascript/rickshaw.js along with d3.layout.js and d3.vs.js. If I save all of these files in the app/assets/javascripts everything works fine, but that does not seem to be the correct rails way.
Does anyone know how to fix this error?
Thank you.
To use the asset pipeline, you'll want just the filename in the require statements:
// Vendor Files
//= require d3.v3
//= require d3.layout
//= require rickshaw
See the asset pipeline docs for more info on asset organization.

Issue with Javascript include in Rails app

I'm using Rails 3.2.8. When the app is deployed access the view that is including a javascript:
<%= javascript_include_tag "epiceditor" %>
Heroku fails with this log:
ActionView::Template::Error (/app/app/assets/javascripts/epiceditor.js.erb has already been required
I've checked some possible solutions, like checking for any reference that may trigger a circular dependency, or simply removing it in case it is being included somewhere else, which isn't. So, if I include it, I get this "has already been included error", if I don't , then the file isn't included at all.
My config/application.rg has this
config.assets.initialize_on_precompile = false
And applications.js has this:
//= require jquery
//= require jquery_ujs
//= require tabs
It might be important to note that the file the tag is referencing is "epiceditor.js.erb", since it has some embedded Rails code that I needed.
Thanks for your help
EDIT:
I believe this is a bug in Sprockets. If I update Rails to 3.2.9rc2, the error is now this:
ActionView::Template::Error (Asset logical path has no extension: epiceditor/.js
but of course the extension in epiceditor is epiceditor.js.erb, and I've tried being explicit about it in the javascript_include_tag as well.
I found the bug.
It turns out that inside the .js.erb file I'm calling
<% asset_path 'epiceditor/' %>
which should expand to the path where all the epiceditor file are placed, but instead is actually loading the file itself in recursive manner. This is expanding properly in the development environment but not in the production environment. Funny, right?
The reason for this is that is adding a digest. So I fixed the whole issue with this:
<%= asset_path 'epiceditor/', :digest => false %>
and now it does expand to the directory, and doesn't fall into the recursion trap.
Hope this saves some time for someone!

Cannot require 'nokogiri' in Rails (but works in irb)

I've just started using Ruby on Rails and so far it's working nicely. I'm now trying to implement a gem but it's not working, and I am hoping it's just a beginner mistake - something which I've not yet grasped!!
I've followed tutorials and got my hello world example - also managed to git push this to my Heroku account. I started to follow the tutorial here: http://railscasts.com/episodes/190-screen-scraping-with-nokogiri and got the following code working in Terminal (on mac)
require 'rubygems'
require 'nokogiri'
require 'open-uri'
url = "http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=batman&Find.x=0&Find.y=0&Find=Find"
doc = Nokogiri::HTML(open(url))
puts doc.at_css("title").text
So that worked nicely. I can see the title inside of terminal. However, when I try to put this code in my view controller, it cannot find the nokogiri gem. The code in my controller is
class HomeController < ApplicationController
def index
require 'rubygems'
require 'nokogiri'
require 'open-uri'
url = "http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=batman&Find.x=0&Find.y=0&Find=Find"
doc = Nokogiri::HTML(open(url))
#mattVar = doc.at_css("title").text
end
end
So then in my HTML, I have
<h1>Hello!</h1>
<p><%= #mattVar %></p>
Which should output the title as it does in the terminal window. However, I get an error saying no such file to load -- nokogiri
Any ideas where I'm going wrong?
If I do gem list I can see the nokogiri gem there.
UPDATE
I closed the local rails server and restarted. It's now working. Not sure if it was because of the work adding the gem to my root or doing the 'bundle install' (as the posts below suggested). Thanks for the help.
Firstly try changing your controller to be somthing like
class HomeController < ApplicationController
require 'nokogiri'
require 'open-uri'
def index
url = "http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=batman&Find.x=0&Find.y=0&Find=Find"
doc = Nokogiri::HTML(open(url))
#mattVar = doc.at_css("title").text
end
end
I'm guessing you are also using bundler, check that you have include this gem and run bundle install.
You can find the documentation for this at http://gembundler.com/
You should put your various require statements outside of your index method. It won't slow you down much to have them in there, but you're asking Ruby to ensure that they are loaded every time the method is called. Better to just require them once at the top of your code. This won't cause your problem, however.
I suspect that you are testing your server under a different environment than what IRB is running in. Try removing the offending require statements for the moment and changing your HTML template to:
<h1>Environment</h1>
<ul>
<li>Ruby: <%=RUBY_VERSION%></li>
<li>Gems: <%=`gem list`%></li>
</ul>

Resources