ruby sinatra_在Ruby中使用Sinatra

ruby sinatra

In the previous article in this series of articles, we talked about what Sinatra is. In this article, we'll look at some real functional Sinatra code, touching on a few Sinatra features, all of which will be explored in depth in upcoming articles in this series.

在本系列文章的上一篇文章中 ,我们讨论了Sinatra是什么。 在本文中,我们将讨论一些实际的Sinatra功能代码,并涉及一些Sinatra功能,所有这些功能都将在本系列的后续文章中进行深入探讨。

Before you get started, you'll have to go ahead and install Sinatra. Installing Sinatra is as easy as any other gem. Sinatra does have a few dependencies, but nothing major and you shouldn't have any problems installing it on any platform.

在开始之前,您必须继续安装Sinatra。 安装Sinatra与其他任何gem一样容易。 Sinatra确实有一些依赖关系,但是没有什么大的依赖,在任何平台上安装它都不会有任何问题。

$ gem install sinatra
$ gem install sinatra

你好,世界! ( Hello, World! )

The Sinatra "Hello world" application is shockingly simple. Not including the require lines, shebang and whitespace, it's just three lines. This is not just some small part of your application, like a controller in a Rails application, this is the entire thing. Another thing you may notice is that you didn't need to run anything like the Rails generator to generate an application. Just paste the following code into a new Ruby file and you're done.

Sinatra的“ Hello world”应用程序非常简单。 不包括require行,shebang和空白,只有三行。 这不仅是应用程序的一小部分,就像Rails应用程序中的控制器一样,这就是全部。 您可能会注意到的另一件事是,您不需要运行诸如Rails生成器之类的任何东西来生成应用程序。 只需将以下代码粘贴到新的Ruby文件中即可。


require 'rubygems'
需要“ ruby​​gems”
require 'sinatra'
需要'sinatra'
get '/' do
得到'/'做
'Hello, world!'
'你好,世界!'
end
结束

Of course this isn't a very useful program, it's just "Hello world," but even more useful applications in Sinatra aren't much larger. So, how do you run this tiny Web application? Some kind of complex script/server command? Nope, just run the file. It's just a Ruby program, run it!

当然,这不是一个非常有用的程序,它只是“ Hello world”,但Sinatra中甚至更有用的应用程序也没有很大。 那么,如何运行这个小型Web应用程序? 某种复杂的脚本/服务器命令? 不,只需运行文件即可。 这只是一个Ruby程序,运行它!


== Sinatra/0.9.4 has taken the stage on 4567 for development with backup from Mongrel
== Sinatra / 0.9.4已在4567上进行了开发,并从Mongrel进行了备份

Not very exciting yet. It's started the server and bound to port 4567, so go ahead and point your Web browser to http://localhost:4567/. There's your "Hello world" message. Web applications have never been so easy in Ruby before.

还不是很令人兴奋。 它启动了服务器并绑定到端口4567,因此继续进行操作,然后将Web浏览器指向http:// localhost:4567 / 。 有您的“ Hello world”消息。 Web应用程序在Ruby中从未如此简单。

使用参数 ( Using Parameters )

So let's look at something a little more interesting. Let's make an application that greets you by name. To do this, we'll need to use a parameter. Parameters in Sinatra are like everything else--simple and straightforward.

因此,让我们看一些更有趣的东西。 让我们创建一个按名称打招呼的应用程序。 为此,我们需要使用一个参数。 Sinatra中的参数与其他所有参数一样-简单明了。


require 'rubygems'
需要“ ruby​​gems”
require 'sinatra'
需要'sinatra'
get '/hello/:name' do
得到'/ hello /:name'做
"Hello #{params[:name]}!"
“你好#{params [:name]}!”
end
结束

Once you've made this change, you'll need to restart the Sinatra application. Kill it with Ctrl-C and run it again. (There's a way around this, but we'll look at that in a future article.) Now, the parameters are straightforward. We've made an action called /hello/:name. This syntax is imitating what the URLs will look like, so go to http://localhost:4567/hello/Your Name to see it in action.

进行此更改后,您将需要重新启动Sinatra应用程序。 使用Ctrl-C将其杀死,然后再次运行。 (有一种解决方法,但是我们将在以后的文章中介绍。)现在,参数很简单。 我们已经执行了一个名为/ hello /:name的操作 。 这种语法模仿的是URL的外观,因此请访问http:// localhost:4567 / hello /您的名称,以查看实际效果。

The /hello portion matches that portion of the URL from the reqest you made, and :name will absorb any other text you give it and put it in the params hash under the key :name. Parameters are just that easy. There is of course much more you can do with these, including regexp-based parameters, but this is all you'll need in almost every case.

/ hello部分与您发出的请求中的URL部分匹配,并且:name将吸收您提供的所有其他文本,并将其放在key :name下的params哈希中。 参数就是那么容易。 当然,您可以使用这些功能做更多的事情,包括基于regexp的参数,但这几乎是每种情况下所需的全部。

添加HTML ( Adding HTML )

Finally, let's spiff this application up with a little bit of HTML. Sinatra will return whatever it gets from your URL handler to the web browser. So far, we've just been returning a string of text, but we can add some HTML in there with no problem. We'll use ERB here, just like is used in Rails. There are other (arguably better) options, but this is perhaps the most familiar, as it comes with Ruby, and will do fine here.

最后,让我们用一点HTML来简化此应用程序。 Sinatra将从您的URL处理程序获得的所有内容返回到Web浏览器。 到目前为止,我们只是返回了一个文本字符串,但是我们可以毫无问题地在其中添加一些HTML。 就像在Rails中一样,我们将在此处使用ERB。 还有其他(可能更好)的选项,但这也许是Ruby附带的最熟悉的选项,并且在这里可以很好地使用。

First, Sinatra will render a view called layout if one exists. This layout view should have a yield statement. This yield statement will capture the output of the specific view being rendered. This allows you to create layouts very simply. Finally, we have a hello view, which generates the actual hello message. This is the view that was rendered using the erb :hello method call. You'll notice that there are no seperate view files. There can be, but for such a small application, it's best to keep all the code in a single file. Though the views are sepeated at the end of the file.

首先,Sinatra将渲染一个名为layout的视图(如果存在)。 此布局视图应具有yield语句。 此yield语句将捕获正在渲染的特定视图的输出。 这使您可以非常简单地创建布局。 最后,我们有一个hello视图,该视图生成实际的hello消息。 这是使用erb:hello方法调用呈现的视图。 您会注意到没有单独的视图文件。 可以存在,但是对于如此小的应用程序,最好将所有代码保存在一个文件中。 尽管视图在文件末尾分开。


require 'rubygems'
需要“ ruby​​gems”
require 'sinatra'
需要'sinatra'
get '/hello/:name' do
得到'/ hello /:name'做
@name = params[:name]
@name = params [:name]
erb :hello
erb:你好
end
结束
__END__
__结束__
@@ layout
@@布局
<html>
<html>
<body>
<身体>
<%= yield %>
<%=收率%>
</body>
</ body>
</html>
</ html>
@@ hello
@@ 你好
<h3>Hello <%= @name %>!</h3>
<h3>你好<%= @name%>!</ h3>

And there you have it. We have a complete, functional hello world application in about 15 lines of code including the views. The following articles, we'll take a closer look at the routes, how you can store and retrieve data, and how to do better views with HAML.

那里有。 我们有一个完整的,功能齐全的hello world应用程序,包含约15行代码,包括视图。 在以下文章中,我们将仔细研究路线,如何存储和检索数据以及如何使用HAML进行更好的查看。

翻译自: https://www.thoughtco.com/using-sinatra-in-ruby-2908286

ruby sinatra

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值