I'm trying to use Chartkick but keep receiving the same error.
"Error Loading Chart: Please install Google Charts or Highcharts"
I am trying to use Google Charts and use the javascript_include_tag at the top of my page where I want the chart like this:
<%= javascript_include_tag "http://www.google.com/jsapi", "chartkick" %>
Not sure why I'm receiving this error and below is also my code requesting the line chart:
<% #station_names = RegAttendee.find(:all, :select => "distinct(check_in_station)", :conditions => {:show_id => 76 }) %>
Thanks for the help
Related
I have implemented the google page speed on rails 4 with the help of pagespeed gem but I am getting my result on console. I want this result on my view page.
Controller code:-
#request = PageSpeed::Request::new("www.google.com", 'api key', 'desktop')
puts #request.pagespeed
The instance variable #request defined in your controller will be available in your views as
<%= #request %>
for pagespeed
<%= #request.pagespeed %>
I am using Rails 4
I followed instruction from https://github.com/pdfkit/pdfkit
In controller I have
respond_to do |format|
format.html {render layout:'application'}
format.csv {send_data #testdatares.to_csv}
format.pdf {
html = render_to_string(:layout => 'application' , :action => "testpage.html.erb");
kit = PDFKit.new(html);
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/application.css";
send_data(kit.to_pdf, :filename => "some_name.pdf", :type => 'application/pdf')
}
end
In application.html.erb I have
<!DOCTYPE html>
<html>
<head>
<title>STNEW</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag "http://www.google.com/jsapi", "/usr/local/lib/ruby/gems/2.1.0/gems/chartkick-1.3.2/app/assets/javascripts/chartkick",media:'all' %>
<%= csrf_meta_tags %>
</head>
Please note that I already used absolute path to chartkick.js
Now in html the chartkick contents were perfectly displayed.
But in the pdf file generated by pdfkit all chartkick contents were shown as "loading", while text was correctly shown
Googled around but found no answer
Finally, after 3 days of search, trails and error, I found solution for this
first of all, I have to use wicked_pdf rather than pdfkit because the former provides two important helper functions
wicked_pdf_stylesheet_link_tag
wicked_pdf_javascript_include_tag
then I must use these two helper to include stylesheet and javascript, like this
<%= wicked_pdf_stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= wicked_pdf_javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag "http://www.google.com/jsapi"%>
<%= wicked_pdf_javascript_include_tag 'chartkick'%>
The difference between javascript_include_tag and wicked_pdf_javascript_include_tag is that the latter will write all original java code into the html file. This is important because wkhtmltopdf, the core of wicked_pdf, does not know where to find the js and css assets. These assets are located at someplace like /usr/local/lib/ruby/gems/2.1.0/gems/ (yours might be different), but only your rails app know where it is
note that for
<%= javascript_include_tag "http://www.google.com/jsapi"%>
you can't and don't need wicked_pdf_javascript_include_tag.
Here the most most important thing come. wicked_pdf (and also pdfkit) will instruct you to install gem wkhtmltopdf-binary , which has the latest version 0.9.9 and then simply call something like this
format.pdf {render pdf :"test",layout:'application',template:'stinfos/testpage.html.erb'}
to generate pdf. This ONLY works for html without chartkick or highchart component. In the PDF file generated, the chartkick or highchart figures will only show as "loading"
To solve this, you need to remove the wkhtmltopdf-binary gem by running
gem uninstall wkhtmltopdf-binary
in your rails app root
Then download the latest version of wkhtmltopdf from http://wkhtmltopdf.org/downloads.html
for me the precompiled version work
http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.1/wkhtmltox-0.12.1_linux-trusty-amd64.deb
and install
dpkg -i wkhtmltox-0.12.1_linux-trusty-amd64.deb
most likely it will be located here /usr/local/bin/wkhtmltopdf
if not, change the config/initializers/wicked_pdf.rb
:exe_path => '/yourpath/wkhtmltopdf'
Then add javascript_delay:2000 to the format.pdf
format.pdf {render pdf:"test",javascript_delay:2000,
layout:'application',template:'stinfos/testpage.html.erb'}
Then you should see the chartkick figures show up in the pdf generated. If not, use a larger delay time.
Note that javascript_delay option is not available for wkhtmlpdf below version 0.10
While WickedPDF probably works just fine, there's a workaround to making PDFKit work as well. You just need to point wkhtmltopdf to the absolute path of the .js file, similar to the approach used for including the CSS files:
html = render_to_string(:layout => 'application' , :action => "testpage.html.erb")
html.gsub!("/assets", "#{Rails.root}/public/assets")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/application.css"
send_data(kit.to_pdf, :filename => "some_name.pdf", :type => 'application/pdf')
The pre-compiled chartkick.js will be in the /public/assets folder and wkhtmltopdf is able to locate it there.
I'm trying to access my page gallery/new via a link, so i have created this
<%= link_to 'New gallery' new_gallery_path %>
rake routes gives
gallery_index_path GET /gallery(.:format) gallery#index
POST /gallery(.:format) gallery#create
new_gallery_path GET /gallery/new(.:format) gallery#new
edit_gallery_path GET /gallery/:id/edit(.:format) gallery#edit
gallery_path GET /gallery/:id(.:format) gallery#show
PATCH /gallery/:id(.:format) gallery#update
PUT /gallery/:id(.:format) gallery#update
DELETE /gallery/:id(.:format) gallery#destroy
and within my routes i have
resources :gallery
My view at gallery/new is
<%= nested_form_for #gallery, :html => { :multipart => true} do |f| %>
--content here
<% end %>
whenever i click the link to view this page i get
undefined method `galleries_path
Can someone point out my mistake, please?
You've chosen the wrong name for your resources. It should always be pluralized:
resources :galleries
From this Rails will generate the plural and singular paths correctly. galleries_path for the index, gallery_path for show, etc etc.
I am trying to use chartkick to plot some charts with the google charts API. The problem is that the line to set everything up is taking 10-20 seconds to load. Here is the relevant line
<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
Has anyone else had this problem? If so how do you fix it?
I don't know why this is, but I managed to get it to go fast by adding http: in the url
<%= javascript_include_tag "http://www.google.com/jsapi", "chartkick" %>
I generally get around this by grabbing a local copy of the file and only using it in development mode.
<% if Rails.env == "development" %>
<%= #include the local file here %>
<% else %>
<%= javascript_include_tag "http://www.google.com/jsapi", "chartkick" %>
<% end %>
Generally seems ok in production mode.
I've been having this annoying problem in which I click over a select dropdown input of my custom form, styled with Zurb Foundation 4 in my Rails application, and the list won't show its elements.
I thought at a start that was a problem with simple form, but I changed the f.association for f.collection_select, my code looks like this:
<h2><%= I18n.t(".sign_up") %></h2>
<%= simple_form_for(resource, :html => {:class => "custom"}, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<%= devise_error_messages! %>
<%= f.input :rut %>
<%= f.input :name %>
<%= f.input :email %>
<div>
<%= f.label :supplier_type_id %>
<%= f.collection_select :supplier_type_id, SupplierType.all, :id, :name %>
</div>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= f.error :base %>
<%= f.submit I18n.t(".sign_up"), :class => "button" %>
<% end %>
<%= render "devise/shared/links" %>
The most strange thing is that sometimes I'm able to see the items when refreshing the page, but when I get to the page navigating from other view of the app then it won't work. I've also noticed this when using hints for forms (i.e: If I have two hints in the same form, in different inputs, only one would show, but the same one message displays in both inputs when each message should show in their respective input. When reloading the page sometimes it shows one hint, and sometimes the other)
The styling looks good, so I think that it might be a foundation javascript problem.
Another thing I've noticed is that when I load the page the styling does a kind of "blinking" when using custom forms. This blinking it looks likes foundation takes a while to load the styling, I've also noticed this on their own custom form documentation site. This may indicate that is a form styled with javascript events or something similar, so this might mean that javascript is working well.
In addition, the checkboxes are having a similar problem, they only can be checked just when you reload the page, it might have some relation with this problem.
I'm very lost, some help would become very handy. Thanks!
--edit: Foundation 5 doesn't include custom forms and works better--
You might need to refresh dropdowns on each page:change event. Try something like this:
$(document).on("page:change", function() {
// SELECTOR_TO_CUSTOM_DROPDOWNS should select any Zurb custom dropdowns you
// are using.
$(SELECTOR_TO_CUSTOM_DROPDOWNS).trigger("change");
});
That's from the documentation on Zurb custom form JS.
Yes, this is caused by turbolinks. It stops $(document).ready from firing on page load, which is required by foundation's custom forms.
Using ssorallen's answer and to be more unobtrusive than the OPs, add this to application.js:
$(function(){ $(document).foundation(); });
$(document).on("page:change", function() {
if ($('form.custom').length > 0) {
$(document).foundation('forms');
}
});
Also, if you have jquery/coffeescript that relies on document ready being fired, add jquery turbolinks to your Gemfile.
Ok, so I just figured out that I have repeated asset load on my browser. This is causing a javascript error. It appears that deleting in the manifest
//= require turbolinks
Solves the problem.
I solved my multiple asset problem changing
<%= javascript_include_tag "application" %>
To the head in my application layout.
After watching the turbolinks railscast I noticed that besides my multiple asset loading, turbolinks and foundation 4 may not be compatible, it might be a solution on this post. But still doesn't work perfect for me.
I also noticed that navbar is also affected by turbolinks.
I think that this is rather a turbolinks problem and not an specific foundation dropdown. I will close this question and open a new on turbolinks and foundation.
Thanks to some Nick Reed insights I found out that the foundation gem was initializing foundation in application.js like this:
$(function(){ $(document).foundation(); });
So I checked the docs and I used this:
<script>
$(document).foundation();
</script>
After the "/body" tag in the application layout and everything seems to be working like a charm!