Ruby and web development

Source: Internet
Author: User

1. Write CGI

A simple CGI program is as follows:

#! /Usr/bin/ENV Ruby
Print "HTTP/1.0 200 okrn"
Print "Content-Type: text/htmlrnrn"
Print "Hello world! RN"

We can use Ruby to process submitted parameters, fill templates, generate HTML, and so on, but it is a little troublesome. We can use the CGI Module.

2. Use CGI. Rb

CGI. Rb is used to write CGI scripts. It can operate form, cookies, environment variables, and maintain sessions. You should know where to find the specific usage.

3. Reference and escape

/The URL contains a special character, including <and>, and sometimes needs to be escaped (escaped), for example,/is converted to % 2f. CGI. RB provides two functions: CGI. Escape and CGI. Unescape:

Require 'cgi'
Puts CGI. Escape ("Nicolas Payton/trumpet & flugel horn ")

Result:

Nicholas + Payton % 2 ftrumpet + % 26 + flugel + Horn

Example of escaping elements in HTML documents (Omitted ):

4. Form

CGI-like can be used to obtain the data submitted by the client in two ways. Joined the user to submit lookup? Player = Tom & year = 1958, which can be accessed directly using the CGI [] array.

Require 'cgi'
CGI = CGI. New
CGI ['player'] #-> ["Tom"]
CGI ['Year'] #-> ["1958"]

Another way is to use a hash table to obtain all parameters and values.

Require 'cgi'
CGI = CGI. New
H = CGI. Params
H ['player'] #-> ["Tom"]

5. Generate form and HTML

CGI class provides a lot of methods for generating HTML, almost every tag is a method. The example is more intuitive:

Require "cgi"
CGI = CGI. New ("html3") # Add HTML generation methods
CGI. out {
Cgi.html {
CGI. head {"N" + CGI. Title {"this is a test" }}+
CGI. Body {"N" +
CGI. FORM {"N" +
CGI. HR +
CGI. H1 {"a form:"} + "N" +
CGI. textarea ("get_text") + "N" +
Cgi.br +
CGI. Submit
}
}
}
}

Generate the following code:

6. Cookies

Require "cgi"
Cookie = CGI: Cookie. New ("rubyweb", "custid = 123", "part = ABC ");
CGI = CGI. New ("html3 ")
CGI. Out ("cookie" => [cookie]) {
Cgi.html {
"Nhtml content here"
}
}

The generated header is as follows:

Content-Type: text/html
Content-Length: 86
Set-COOKIE: rubyweb = custid % 3d123 & Part % 3 dabc; Path =

Then, when you access the page again, you can read this value.

Require "cgi"
CGI = CGI. New ("html3 ")
CGI. out {
Cgi.html {
CGI. Pre {
Cookie = CGI. Cookies ["rubyweb"]
"Ncookies aren" + Cookie. value. Join ("N ")
}
}

7. Sessions

To process a session, an additional class is required: CGI: Session.

Require "cgi"
Require "cgi/session"

CGI = CGI. New ("html3 ")
Sess = CGI: Session. New (CGI, "session_key" => "rubyweb ",
"Session_id" = & gt; "9650 ",
"New_session" => true,
"Prefix" => "Web-session .")
Sess [& quot; custid & quot;] = 123
Sess ["part"] = "ABC"

CGI. out {
Cgi.html {
"Nhtml content here"
}
}

Read the session using the following code:

Require "cgi"
Require "cgi/session"

CGI = CGI. New ("html3 ")
Sess = CGI: Session. New (CGI, "session_key" => "rubyweb ",
"Prefix" => "Web-session .")
CGI. out {
Cgi.html {
"Ncustomer # {sess ['lastid']} orders an # {sess ['part']}"
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.