ruby cgi按钮_Ruby中的CGI编程

ruby cgi按钮

Ruby CGI程式设计 (Ruby CGI programming)

Before getting into what CGI programming means in Ruby, let us understand what it means the real programming world. CGI is an abbreviation for Common Gateway Interface. It is identification for conveying data or information between the World Wide Web and a CGI program. CGI programs provide a dynamic way to interact with the user and designed in a way that they can accept and return data as well. All the processing occurs at the Web Server; hence it is used as a Server-Side solution.

在了解CGI编程在Ruby中的含义之前,让我们了解它在真正的编程世界中的含义。 CGICommon Gateway Interface的缩写。 它是用于在万维网和CGI程序之间传送数据或信息的标识。 CGI程序提供了一种与用户互动的动态方式,并且设计为可以接受和返回数据的方式。 所有处理都在Web服务器上进行。 因此,它被用作服务器端解决方案。

Now, let us understand the way to write CGI programs in Ruby.

现在,让我们了解在Ruby中编写CGI程序的方式。

If you want to generate HTML output from a Ruby script, you can write the following code:

如果要从Ruby脚本生成HTML输出,可以编写以下代码:

print "HTTP/1.0 200 OK\r\n"
print "Content-type: text/html\r\n\r\n"
print "<html><body>Hello World!</body></html>\r\n"

Output

输出量

HTTP/1.0 200 OK
Content-type: text/html
<html><body>Hello fella! Greetings from IncludeHelp.com</body></html>

cgi.rb is a predefined class termed as cgi helps you to write CGI programs. You can create forms, cookies, maintain stateful sessions and many more, with the support of cgi class. You have to give the statement require cgi at the top of the program to enjoy the capabilities of cgi class.

cgi.rb是一个预定义的类,称为cgi,可帮助您编写CGI程序。 您可以在cgi class的支持下创建表单,Cookie,维护状态会话等。 您必须在程序顶部给出要求require cgi的语句,才能享受cgi类的功能。

Let us see some of the abilities of CGI class.

让我们看看CGI类的一些功能。

Quoting

报价单

If are writing a URL or an HTML code, you are supposed to be very careful with characters. Many characters have special meaning in URLs. Translations of some of the characters are given below:

如果要编写URL或HTML代码,则应谨慎对待字符。 URL中许多字符都有特殊含义。 以下是一些字符的翻译:

  • / => %2F

    / =>%2F

  • & => %26

    &=>%26

  • <space> => +

    <空格> => +

To handle the above characters, cgi provides you .escape and .unescape. They are routines of cgi. See the example given below for reference:

为了处理上述字符, cgi为您提供了.escape.unescape 。 它们是cgi的例程。 请参阅下面的示例以供参考:

require 'cgi'
puts CGI.escape( "He/She must be aware of his/her capabilities & should work accordingly" )

Output

输出量

He%2FShe+must+be+aware+of+his%2Fher+capabilities+%26+should+work+accordingly

The following code will let you know how you can escape HTML special characters?

以下代码将使您知道如何转义HTML特殊字符?

require 'cgi'
puts CGI.escapeHTML( '<a href="/san">Go to the site(Includehelp.com)</a>' )

Output

输出量

&lt;a href=&quot;/san&quot;&gt;Go to the site(Includehelp.com)&lt;/a&gt;

创建表单和HTML (Creating forms and HTML)

cgi class is an umbrella of the huge number of methods which are necessary to write HTML. Every tag has got a method under cgi. cgi is a class and we all that to access the methods of a class, first we have to instantiate it. cgi can be instantiated by calling CGI.new.

cgi类是编写HTML所需的大量方法的保护伞。 每个标签在cgi下都有一个方法。 cgi是一个类,要访问一个类的方法,我们必须首先实例化它。 可以通过调用CGI.new实例化cgi。

Let us design a basic form with the help of code mentioned below,

让我们借助下面提到的代码设计基本表单,

require "cgi"
cgi = CGI.new("html5")
cgi.out {
   cgi.html {
      cgi.head { "\n"+cgi.title{"It is an Example of form"} } +
      cgi.body { "\n"+
         cgi.form {"\n"+
            cgi.hr +
            cgi.h1 { "A Form: " } + "\n"+
            cgi.h2 { "A Basic Form:"} + "\n" +
            cgi.textarea("get_text") +"\n"+
            cgi.button("click_here") +"\n"+
            cgi.hr +
            cgi.br +
            cgi.submit
         }
      }
   }
}

Output:

输出:

Content-Type: text/html
Content-Length: 317

<!DOCTYPE HTML><HTML><HEAD>
<TITLE>It is an Example of form</TITLE></HEAD><BODY>
<FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">
<HR><H1>A Form: </H1>
<H2>A Basic Form:</H2>
<TEXTAREA NAME="get_text" COLS="70" ROWS="10"></TEXTAREA>
<BUTTON></BUTTON>
<HR><BR><INPUT TYPE="submit"></FORM></BODY></HTML>

The above code is producing an HTML form having title "This is an example". The form has horizontal rulers, Headings, two Input controls namely TextArea and button. All the code blocks will return a string which will be utilized as the content for the tag.

上面的代码正在生成一个标题为“ This is a example”HTML表单。 表单具有水平标尺,标题,两个输入控件,即TextArea和button。 所有代码块都将返回一个字符串,该字符串将用作标签的内容。

翻译自: https://www.includehelp.com/ruby/cgi-programming-in-ruby.aspx

ruby cgi按钮

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值