Sinatra and Chartkik example

I'm brand new to Ruby as a web platform, and I’m best involved in adapting and expanding my examples. I like Sinatra and I want to use it and Chartkick to display charts. Unfortunately, I had problems running even the most trivial example, and there seems to be a lack of complete examples (many "one liner"). Here is what I have:

require 'sinatra'
require 'chartkick'

get '/' do
'<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
 <%= pie_chart({"Football" => 10, "Basketball" => 5}) %>'
end

So what am I implausible? Any help would be greatly appreciated. Thank!

EDIT, my decision is based on the accepted answer:

require 'sinatra'
require 'chartkick'

template :layout do
<<LAYOUT
<html>
<head>
<title>Sinatra ERB Chartkick Test</title>
<script src="//www.google.com/jsapi"></script>
<script src="chartkick.js"></script>
</head>
<body>
<%= yield %>
</body>
</html>
LAYOUT
end

template :index do
<<INDEX
<center>
<h1>#{Time.now.inspect}</h1>
<p>
<%= pie_chart({"Football" => 10, "Basketball" => 5, "Hockey" => 2}) %>
</p>
</center>
INDEX
end

get '/' do
    erb :index
end

Also, for beginners (like me), I put the chartkick.js file in a shared folder. Hockey has also been added to make sure that it is not magically inappropriate for some canned foods. And who doesn't like hockey?

+4
1

, . , , , .

get '/' do
  erb :index
end

__END__

@@ layout
<html><body>
  <%= yield %>
</html></body>

@@ index
<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
<%= pie_chart({"Football" => 10, "Basketball" => 5}) %>

:

template :layout do
  <<LAYOUT
<html><body>
  <%= yield %>
</html></body>
LAYOUT
end

template :index do
  <<INDEX
    <%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
    <%= pie_chart({"Football" => 10, "Basketball" => 5}) %>
INDEX
end

get '/' do
  erb :index
end

heredocs , .

+2

Source: https://habr.com/ru/post/1537649/


All Articles