[Ruby on Rails series]3, initial rails: Developing the first Web program using Rails

Source: Internet
Author: User
Tags ruby on rails

The first two sections of this series have already covered how to configure the Ruby on Rails development environment, and now it's time to get to the point!

Part1. Pre-development Preparation The main task of this time is to develop the first rails program. In particular, this time I chose a (PAAs development platform), which is the Cloud 9 platform described in the Rails tutorial, which has automatically made our environment configuration work, as long as you have a browser to use the cloud development environment. Very convenient and quick! It's amazing! The platform URL is as follows:https://c9.io/

The essence of the Cloud 9 development platform is to allocate a Linux virtual machine on the server side for each registered developer, so developing on cloud 9 is like developing on a virtual machine. The terminal input command in Cloud 9 is actually exactly the same as typing the command on Linux. Therefore, this article also applies to local development.

How to use Cloud 9:

(1) Sign up for a free account and log in

(2) Click "Go to your Dashboard"

(3) Click "Create New Workspace"

(4) Select a workspace type and name it,

(5) Click "Create"

(6) Click "Start Editing" to enter the virtual machine.

(7) After entering the development environment, you can set up the development environment according to your own development habits (explore ~)

(8) Install rails. The Cloud 9 development environment contains all the tools we need, but without the rails itself, open the terminal and enter the following code to install rails.

Now that the development environment is ready, the terminal input rails--version can check the rails version to verify that the installation was successful.

Part2. First Application 

Once the development environment is configured, you can develop your first application.

(1) Create project, enter in Terminal:rails new (project name)

Rails New Firstdemo

After waiting a few seconds, the following code is displayed if the creation succeeds:

Your Bundle is complete! Use ' bundle show [Gemname] ' to see where a bundled gem is installed.         Run  bundle EXEC Spring binstub--all* bin/rake:spring inserted* bin/rails:spring inserted

at this point, rails automatically creates the file structure and initializes the template according to the MVC model. The file structure after creation is as follows:

Note: If you do not create a successful, please do not worry, it may be some dependent file virtual machine is not the default, then you need to follow the failure of the prompt information, install the corresponding dependent package. Rebuild after installation is complete.

(2) Modify the Gemfile file and rebuild

, open the Gemfile, remove the # Gem ' Therubyracer ', platforms:: Ruby the line comments to ensure that our engineering supports JavaScript. If you do not comment on this line, then the build will error: The JavaScript platform is missing.

After the comments are complete, enter the following code in the terminal to rebuild: (Switch to the project folder before building)

CD Firstdemo
Bundle Install

(3) using the automation framework provided by Rails scaffold directives

Enter the following command at the terminal:

Rails G Scaffold Memo Content:text

At this point, rails creates a memo table that has a text type of field content. At the same time, rails builds the relevant presentation layer, control layer, and data layer code for us. Specific grammatical details, I will study after, now do not care.

We can open the App/views/memos folder to see the specific code generated.

(4) Establishing a database

Enter the following command in the terminal to establish the database:

Rake Db:migrate

SqlLite3 automatically creates a database based on the data model.

(5) Turn on the server and run the Web program

Note: Programs created with Cloud 9 and native-created projects will be different in this step.

If you are using a local server. In Terminal input:

Rails Server

then open the browser and enter: Http://127.0.0.1:3000/memos, the test is running successfully.

If you use Cloud 9, you need to enter it at the terminal:

Rails server-b $IP-P $PORT

Then, depending on the Cloud 9 prompt, open the appropriate page.

(6) View results

Note that directly click on the above link to enter is not our generated page, please add/memos after the generated link (reason please carefully study the Config folder route.rb)

Running results such as:

Our first Rails-based Web application is now complete. No code at all! The development efficiency of the rails framework is fully demonstrated.

PostScript

Because it was the first rails program, we didn't need to delve into Ruby's syntax details on rails, and we used the build tool provided by rails to quickly generate applications that contained databases, which is much faster than other development frameworks, scaffold. I've been studying for 2 months. JSP will not write database-based web programs ...

Using the auto-build framework ignores many details, and later in the study, I'll delve into the syntax and design of the rails framework, rather than building an application as quickly as it is now.

In the next article, I'll explore a special issue: the Internationalization of rails applications, so stay tuned.

[Ruby on Rails series]4, featured: Internationalization of rails applications [i18n]

[Ruby on Rails series]3, initial rails: Developing the first Web program using Rails

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.