Create an application instance in rails

Source: Internet
Author: User

After installing the Rails framework, you also get a new command line tool: rails. This tool can be used to construct each new Rails application.

Why do we need such a tool-I mean, why don't we just copy the most handy editor and write each line of application code from the beginning? Er ...... We can do this, But Rails can do a lot of tricks behind the scenes, so that we can run an application with only a few configurations. To make these tricks take effect, Rails must be able to find various components in the application. As we will see later, this means that we must create a fixed directory structure and place our code in a proper place. The rails command can help us create this directory structure and generate some standard Rails code.

Now, let's create the first Rails application: Open the shell window and enter somewhere in the file system-you want to save the application directory structure somewhere there. In our example, we will create the project under a directory named work. Therefore, we use the rails command in this directory to create an application named demo. Be careful: If a directory named demo already exists, rails will ask you if you want to overwrite the existing file.
Copy codeThe Code is as follows:
Dave> cd work

Work> rails demo

Create

Create app/apis

Create app/controllers

Create app/helpers

:::

Create log/development. log

Create log/test. log

Work>

The preceding command creates a directory named demo. Go to this directory and list all its contents (use the ls command in Unix and the dir command in Windows). You should see a bunch of files and subdirectories like this:

Copy codeThe Code is as follows:
Work> cd demo

Demo> ls-p

CHANGELOG app/db/log/test/

README components/doc/public/vendor/

Rakefile config/lib/script/

Suddenly you may feel a little scared to face so many directories (and the files they contain), but we don't have to worry about their existence. Now, we only need to use one of them, that is, the public directory.

As its name implies, the public directory contains the files we want to expose to end users. The key files here are dispatcher: dispatch. cgi, dispatch. fcgi, and dispatch. rb. The distributor is responsible for receiving user requests from the browser and directing these requests to the program code in the application. These files are important, but we do not need to contact them yet.

You will also see that there is a script subdirectory under the demo Directory, which stores some tool scripts, which will be used during application development. Now, we need to use the script named server, which will start an independent WEBrick [1] server, and the newly created Rails application will run in it. Before proceeding, start the application you just wrote (or generated.
Copy codeThe Code is as follows:
Demo> ruby script/server

=> Rails application started on http: // 0.0.0.0: 3000

[09:16:43] INFO WEBrick 1.3.1

[09:16:43] INFO ruby 1.8.2 () [powerpc-darwin7.5.0]

[09:16:43] INFO WEBrick: HTTPServer-start: pid = 2836 port = 3000

From the last line of the startup output, we can see that we started a web server on port 3000 [2. We can open the browser and access http: // localhost: 3000. Then we can see this application.
We can keep WEBrick running in this command line window. After writing the application code, you can access the code in a browser and view the request information in the command line window. To stop running WEBrick, press Ctrl-C in the command line window.

Now, we have run the new application, but there is no code written by ourselves. Next, we need to change this situation.

[1] WEBrick is a web server written in Ruby only. It is released with Ruby 1.8.1 or later.
[2] "0.0.0.0" in the URL indicates that WEBrick will receive connections from all interfaces. In Dave's OS X system, this indicates that all requests from local interfaces (127.0.0.1 and: 1) or from LAN connections will be received by WEBrick.

Now, let's write an extremely simple web application to verify that Rails has successfully settled on our machine. In this process, we will also briefly introduce how Rails Applications work.

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.