Oracle Application of Ruby on Rails (1)

Source: Internet
Author: User

Rails is an easy-to-use framework, but like any technology, it takes some time to master it. You may have heard of Ruby on Rails. This new application framework is sweeping the Web development community, especially in the J2EE and PHP programming circles.

Rails is a powerful Web application platform, which won the favor of many J2EE and PHP programmers in less than two years. Why does it get the favor of J2EE and PHP programmers? When you understand the advantages of Rails, your questions will be blank. First, it uses a strict model-view-controller (Mode-view-controller) architecture, I have won the respect of self-esteem and self-confidence in the design model. This is why J2EE developers have a special liking for it. The second is that using Rails can easily build the basic system-this is why it attracts PHP developers.

However, from the database perspective, Rails has some obvious defects. Rails makes many assumptions about database layout and application requirements. For example, Rails assumes that all tables use a non-composite primary key. It does not support compound primary keys! In addition, Rails does not support two-step commit; it can only be used for a single database backend.

This article is not a comment on Rails or a critical article, but an introduction to this technology. There are both comments and criticisms. Sometimes criticism may seem a little harsh (especially in the eyes of Rails enthusiasts), but do not be confused by appearances. Be skillful in using any Web application framework, whether it is J2EE, ASP. NET, or PHP. In the long run, using Rails is more efficient than using other Web application development platforms, but it takes some time to get familiar with and master this technology.

What is Ruby? What is Rails?

Ruby is a dynamic programming language that is similar to Perl, Python, and Smalltalk in many aspects, but it is self-contained. Java and PHP programmers seem to be able to easily master this language, and they are Ruby's biggest supporter. This language also attracts others, including Don Box, the Microsoft super "hacker ".

Ruby's history is not short. It came out in 1993 and was known to the public for Java and PHP in 1995. So although Ruby is a mature language, it is not as popular as other languages. This makes the Ruby programming language camp not as powerful as the Java, PHP, and Perl camps. Therefore, you will find that there are few frameworks, libraries, books, websites, blogs, and other resources dedicated to this language.

After David Heinemeier Hanson launched the Rails framework recently, the poor Ruby community promotion capability has been reversed, and there is evidence that the framework is the most popular application since the birth of Ruby. Rails extends the Ruby programming Language to a Domain-Specific Language that is extremely suitable for Web development ). It also uses the ActiveRecord extension to introduce object link persistence to the Ruby language. The biggest obstacle to Rails is that you need to re-learn a programming language to use it, except that it does not support compound primary keys and two-step commit. This is not a stage that most people can skip. learning a new language can bring you joy, but it is also a daunting journey.

However, if you want to add a strong emphasis on your resume, the Ruby language is the best choice. Plum Blossom is from bitter cold, Ruby is a language that has never been affected by commercial interests and has been honed for 10 years. Therefore, using Ruby can bring you a pleasant experience. In many cases, its productivity is an order of magnitude higher than that of Java, C, and Microsoft. NET languages. (IronPython may be an exception. It is an interface between the Python language and the Microsoft. NET environment. Python is similar to Ruby and is also a highly productive dynamic programming language .)

What is the purpose of Rails?

Rails is a framework designed from scratch to build dynamic websites using the relational database backend. It introduces keywords to the Ruby programming language and simplifies the configuration of Web applications. In addition, it can automatically generate a complete Web application based on the existing database mode, although a little rough. This capability is both a prominent advantage of Ruby and a critical weakness of Ruby. Rails assumes that the database mode naming convention is followed. If this rule is followed, you only need to execute a command to generate a basic Web site. However, some additional configurations are required to complete this task. Sometimes these configurations may not be implemented at all. You will also find that the database specifications assumed by Rail are almost changeable, but the more you change, the lower the productivity of the platform. This is why Rails is so powerful when developing new applications for new databases. However, it is not a good solution to deal with the original system.

The example is the best way to develop new applications using Rails. If you have an Oracle database installed and can create a new database and a new table based on the database, it takes no more than 15 minutes to create a complete Web application. In this example, we will use Ruby on Rails to build a product directory.

Example: simple product catalog

I have invested in high-end comic books since last year, especially the old versions since 1946. I have created a simple Ruby on Rails database application that allows me to easily insert, remove, modify, and delete books during a sales activity. Because it is for my personal use, the program can naturally be reduced or reduced. The application to be built in this article is the "embedded" version of my Ruby on Rails application.

Although Rails and Oracle databases can be used on many platforms, this article uses Windows XP, and there is evidence that it is one of the most common platforms for Oracle application developers. This example is very simple. It is actually the database version of the "Hello World" programming language example. It aims to give you a first taste of Ruby, but it does not show all Rails capabilities, just as the "Hello World" example of any programming language does not fully demonstrate all the capabilities of the language.

If you encounter problems when running this example, there may be three reasons: the downloaded code is incorrect; the database is improperly set and cannot be connected to this example application; or a problem occurs in your Windows XP operating system. During the development of this example, I encountered some problems related to version and Database Configuration-all of which were caused by lack of knowledge. I learned a lot from my friend Michael Carland so that this example can be completed smoothly and made as simple as possible.

Actually, implementing Ruby on Rails and Oracle Collaboration is not a simple task. Sometimes it is frustrating. As described in this article, you will not encounter any problems. However, if it exceeds the scope of this example, problems may occur. Rails can achieve high productivity, but it is a command line development environment and does not provide the "Automation" function that is commonly used in the advanced integrated development environment. If you are not a self-reliant technician, it is best not to use Rails for the moment.

Step 2: Set Oracle Database

If you are not interested in using Rails and Oracle in combination, you will not read this article. Therefore, you need to install an Oracle database instance and add a table to the database. It is very important to strictly follow the naming rules in this article, otherwise the code automatically generated by Rails will not run properly (if you do not adjust it ). The table you want to create is described as follows:

CREATE TABLE comics (
id, NUMBER(10) NOT NULL,
title VARCHAR2(60),
NUMBER(4),
publisher VARCHAR2(60),
PRIMARY KEY (id)
);
CREATE SEQUENCE comics_seq;

An SQL script file is provided here. You can use it to create tables and add some product data. I recommend that you use it so that your application will be consistent with the one developed here. The easiest way to do this is to follow the steps below to use SQL * Plus. (Assume that you have installed an Oracle database and know how to use it .)

Use SQL * Plus to create a user with DBA permissions for operations in this application.

SQL>GRANTdbaTOrubyIDENTIFIEDBYruby; 
SQL>ALTERUSERrubyDEFAULTTABLESPACEusersTEMPORARYTABLESPACEtemp;
SQL>EXIT

Use the SQL file to create the application table COMICS and insert some sample data.

 C:\>sqlplusruby/ruby@rails@comics.sql 

You may have discovered that the COMICS created in this example is in the plural form, which does not seem quite normal, because we usually name the table as a singular form (such as COMIC ). However, Rails uses the plural form to name tables. You can use the Rails configuration to change this default specification, but it is easier to use the plural name.

Step 2: Install Ruby, RubyGems, Rails, and Rails Oracle Libraries

To use Rails on Windows XP, two software is required: the Ruby programming language installation package with RubyGems, Rails 1.0 (Version 1.1 was launched not long ago, but this article only discusses 1.0 ), and Rails Oracle database. To download and install the Ruby installer with Gems, follow these steps.

  • Visit http://rubyinstaller.rubyforge.org /.
  • Download "One-Click Ruby Installer ".
  • Follow the instructions to complete the installation.

After installing the Ruby programming platform, you can use the Ruby application tool RubyGems to easily install the Ruby framework. To install the Rails application development framework, open a command line prompt and Type A command:

 C:\>gemsinstallrails-v1.0.0--remote 

Now that you have successfully installed Ruby, RubyGems, and Rails, you can install some specialized code libraries to implement communication between the Rails application and the Oracle Database Server. To download and install the Oracle Rails library, visit http://rubyforge.org/projects/ruby-oci8 to download the ruby-oci8-0.1.13-mswin.r to the c drive, and then execute the following command in the Command window:

 C:\>rubyruby-oci8-0.1.13-mswin.rb 

If everything goes well, you have now installed the Ruby, Rails, and Rails-Oracle connection libraries. The next step is to create your first Ruby on Rails Web application.


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.