从零开始制作没有任何框架的Ruby网页 [英] Ruby web pages without any framework from scratch

查看:33
本文介绍了从零开始制作没有任何框架的Ruby网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从头开始学习 ruby​​ for web.我知道 PHP,我是 ruby​​ 新手.一开始我不想使用任何框架.尝试创建简单的 2 - 3 页用于练习.我搜索了很多,但没有任何示例可以从 ruby​​ 和 racks 创建网页并在任何浏览器中运行它.

I'm trying to learn ruby for web from scratch. I know PHP and I'm new in ruby. I don't want to use any framework at beginning. Trying to create simple 2 - 3 pages for practice. I search a lot but not got any example to create webpage from ruby and racks and to run it in any browser.

伙计们,如果您有任何开始的 URL 或在线材料,请分享.

Guys please share if you do have any URL or online material to get start.

推荐答案

用 Ruby 编写一个简单的 CGI 很容易,但是解释如何超出 Stack Overflow 的范围.

Writing a simple CGI in Ruby is easy, however, explaining how is beyond the scope of Stack Overflow.

首先,阅读维基百科的通用网关接口文章,以便您对什么是CGI必须回归.挑选 Perl 示例;你应该能够很容易地弄清楚发生了什么.通读文章并将其内容与代码示例的内容进行比较.

To start, read Wikipedia's Common Gateway Interface article so you have a basic idea of what a CGI has to return. Pick apart the Perl example; You should be able to figure out what's going on easily enough. Read through the article and compare what it says to what the code example is doing.

接下来看Ruby的CGI 模块,因为它将帮助您解析传入的表单信息,并编码/解码 URL 和 HTML.查看环境变量表,您将看到传入的内容与维基百科文章中提到的内容相同.接下来,注意 params() 方法,它是您访问表单变量的方式.通过阅读Writing output"和Generating HTML"部分,并在示例中了解一下.那你就该上路了.

Next, look at Ruby's CGI module, because it will help you parse incoming form information, and encode/decode URLs and HTML. Look at the table of environment variables, and you'll see the same things passed in that are mentioned in the Wikipedia article. Next, notice the params() method, which is how you access form variables. Follow that by reading the "Writing output" and "Generating HTML" sections, and nose about in the examples. You should be on your way then.

完成此操作后,您应该查看 Sinatra gem.这是一个非常好的、易于使用的框架,非常适合一般的 Web 使用,并且在了解 CGI 的工作原理后,您将处于一个很好的位置来欣赏 Sinatra 为您所做的事情.此外,我强烈建议将 HAML 与 Sinatra 一起使用.它是一种生成网页的快捷语言,类似于 ERB,但不那么冗长.

Once you've done that, you should take a look at the Sinatra gem. It's a very nice, easy to use, framework that is well suited for general web use, and, after getting an understanding of how a CGI works, you'll be in a good place to appreciate what Sinatra does for you. Additionally, I highly recommend using HAML with Sinatra. It's a short-cut language for generating web pages, similar to ERB, but less verbose.

您会发现 PHP 为您提供了大量使用 CGI 编写普通"Web 应用程序所需的知识.如果您已经知道这些东西那没关系,但是如果您从未编写过较低级别的代码,或者正在尝试使用不同的语言,那就不好了,因为了解 CGI 层提供了理解整个 HTTPD 堆栈所需的大量知识.一旦您了解了 CGI 层,诸如 Sinatra、Rails、Django、Mojolicious 和所有其他框架之类的东西就会突然变得更有意义,您就会知道何时利用它们或推出自己的框架.而且,PHP 在 HTTP 服务器中的位置也将更加清晰,让您可以更好地为手头的工作挑选工具.

You'll find that PHP has shielded you from a lot of the knowledge needed to write a "normal" web application using a CGI. That's OK if you already know that stuff, but it's bad if you have never done lower-level code, or are trying to use different languages, because knowing the CGI layer provides a lot of knowledge that is needed to understand the whole HTTPD stack. Once you know the CGI layer, things like Sinatra, Rails, Django, Mojolicious and all the other frameworks out there suddenly make more sense, and you'll know when to take advantage of them or roll your own. And, PHP's place in the HTTP server will be clearer too, allowing you to pick and choose your tools better for the job at hand.

这篇关于从零开始制作没有任何框架的Ruby网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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