Sinatra和Chartkick示例 [英] Sinatra and Chartkick Example

查看:71
本文介绍了Sinatra和Chartkick示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Ruby作为Web平台,我还很陌生,可以通过改编和扩展示例来学习最好。我喜欢Sinatra,并希望使用它和Chartkick来显示图表。不幸的是,即使是最琐碎的示例也无法运行,而且似乎缺少完整的示例(很多单眼皮)。这就是我所拥有的:

I am quite new to Ruby as web platform and learn best by adapting and expanding examples. I like Sinatra and want to use it and Chartkick to display charts. Unfortunately, I'm having trouble getting even the most trivial example running and there seems to be a dearth of full examples (lots of 'one liners'). Here's 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 devastatingly simple thing am I doing wrong? Any help would be greatly appreciated. Thanks!

编辑,我的解决方案基于可接受的答案:

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

另外,为了新手(像我一样),我将 chartkick.js 文件放入 public 夹。还添加了曲棍球,只是为了确保它不是神奇地默认为某些罐头示例。谁不喜欢曲棍球呢?

Additionally, for the sake of newbies (like me), I put the chartkick.js file in the public folder. Also added Hockey just to make sure it wasn't magically defaulting to some canned example. And who doesn't like hockey?

推荐答案

这是完成本示例所需的文档部分。如果您不想将视图分开放置文件,则可以使用内联模板,例如

This is the section of the docs you need to finish this example. If you don't want to make the views separate files, then either use inline templates, e.g.

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

我用过此处文档为了方便起见,请小心格式化。

I've used heredocs for convenience here, just be careful of the formatting.

这篇关于Sinatra和Chartkick示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆