Rails creates an application instance _ruby topic

Source: Internet
Author: User

After you install 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 not copy the most convenient editor and start writing every line of the application from scratch? Uh...... We can do that, but rails can do a lot of tricks behind the scenes, so that we just need to do a minimal amount of configuration to run an application. In order for these tricks to take effect, rails must be able to find various components in the application. As we'll see later, this means we have to create some sort of fixed directory structure and put our code in the right 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 go somewhere in the filesystem--you want to keep the application directory structure there somewhere. In our example, we will create the project under a directory named work. So, we create an application named demo in this directory with the Rails command. Be careful here: if you already have a directory named demo, Rails asks if you want to overwrite existing files.

Copy Code code 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 above command creates a directory named demo. To enter this directory, listing all of its contents (using the LS command in Unix, using the dir command in Windows), you should see a bunch of files and subdirectories:

Copy Code code as follows:

Work> CD Demo

Demo> ls-p

Changelog app/db/log/test/

README components/doc/public/vendor/

Rakefile config/lib/script/

It may be a bit scary for you to suddenly face so many catalogs (and the files they contain), but we don't have to ignore them. Now, we just need to use one of them, the public directory.

As its name implies, the public directory contains the files that we want to expose to the end user. The key files here are the distributors (dispatcher): dispatch.cgi, dispatch.fcgi, and DISPATCH.RB. The Distributor is responsible for receiving requests from users from the browser and directing those requests to program code in the application. These files are important, but we don't have to touch them yet.

You'll also see that there's a script subdirectory in the demo directory with some tool scripts that we'll use when developing the application. Now we're going to use the script named Server, which starts a stand-alone webrick[1 server, where our new rails application will run. So, before we move on, let's start with the application that we just wrote (or build).

Copy Code code as follows:

Demo> Ruby Script/server

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

[2005-02-26 09:16:43] INFO Webrick 1.3.1

[2005-02-26 09:16:43] INFO Ruby 1.8.2 (2004-08-24) [powerpc-darwin7.5.0]

[2005-02-26 09:16:43] INFO webrick::httpserver-start:pid=2836 port=3000


As you can see from the last line of boot output information, we started a Web server on port 3000 [2]. We can open the browser, visit the http://localhost:3000, we will see this application.
We can keep Webrick running in this command line window all the time. After we write the application code later, as soon as we access it in the browser, we see information about the Command Line window output request. If you want to stop Webrick running, you can press the CTRL-C key in the Command line window.

Now we've got the new app running, but there's no code that we've written ourselves. Now we're going to change that.

[1] Webrick is a Web server written in pure Ruby, released with Ruby 1.8.1 or a later version.
[2] the "0.0.0.0" in the URL address indicates that Webrick will receive connections from all interfaces. On Dave's OS X system, this means that requests from the local interface (127.0.0.1 and:: 1) or from a LAN connection are Webrick received.

Now, let's write a very simple Web application to verify that rails has successfully settled on our machine. In this process, we'll also brief you on how the rails application works.

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.