Rails is an Open source using Ruby's framework for developing web applications, which uses popular MVC patterns. This article describes a simple database-based web application development.
Translation from http://www.rubyonrails.org/show/tutorialstepone h h-.org
The original text is six steps six pages, which is simplified here.
1. Create a database
Create a database called Rails_Production, as well as users who have access to database permissions. This example database is as follows: # # 数据 数据: `rails_production` #
# ------------------------------------------------- -------
## Table `People` define #
Create Table `People` (` ID` int (10) unsigned not null auto_increment, `Name` VARCHAR (50) Not null default '',` Street1` Varchar (70) Not null default ', `Street2` Varchar (70 NOT NULL Default ', `City` VARCHAR (70) Not null default',` State` Char (2) Not null default ', `zip` Varchar (10) Not null default', Primary Key (` ID`), Key `Name` (` Name`) Type = Myisam Auto_Increment = 2;
## is `people` Insert data #
Insert Into `People` Values (1, 'Superman', '123 Somallwhere', '', 'Smallville', 'KS', '123456');
2. Setting the database parameter editing config / database.yml (tomorrow I do something in the configuration file * .zml :-)), the corresponding example is as follows:
My SQL: Production: Adapter: mysql Database: Rails_Production Host: localhost Username: root password: SQLiteProduction: Adapter: SQLite DBFile: ../db/rails-production.db
Test: Adapter: SQLite DBFile: ../db/rails-test.dbpostgre SqlProduction: Adapter: PostgreSQL Database: Rails_Production UserName: Postgres Password:
Test: Adapter: PostgreSQL Database: Rails_Test Username: Postgres Password:
3. Creating a controller If you have not created a Rails app, you must first create it. Such as: rails path / to / you / new / railsapp [Note] This will delete existing Rails applications in this directory. Create a new controller with new_controller (see the documentation can run it without parameters). This is: ./ script / new_controller friend Friends List Display New Edit Note: New_controller and new_model (see below) The command includes the row of the Ruby path setting, if you need to change these parameters, you can open these files directly Modify, set the correct location of Ruby. Note: Don't define any action called 'Process', this method is that the system must work correctly, you can't overload it. Note: Windows users need to run the program. Ruby Script / New_Controller Friends List Display New Edit4. Creating a model model Create a model with command script / new_model, similarly, without parameters to view the help documentation of this command. ./script/new_model Person5. Then enter the Rails directory. If the RAKE program is installed, run "rake". This will test if your Rails app is properly deployed. Note: In version 0.5.0, this test only supports Mysql, so if you use SQLITE or PSQL error, you don't have to worry.
6. Develop RAILS App Open Views / Friends / Display.rtml files, of course, in the Rails application directory. This file is automatically generated in the first few steps, and we need to fill something in the inside. After the revision is followed:
body> html>
There are some dynamic content in this, where the @Person object requires a controller to create, so open controllers / friends_contrller.rb. Some methods have been automatically created, but Model has not been connected, so add: Require 'Person' this will introduce our definition Active Record class, and we can access the database. Find a method called Display, change to the following: Def Display @Person = person.find (1) End
This will select the first record from the database and put it in the @Person variable for View. Now let's take a look at the Model class, open the Models / Person.rb file. The same system has made a lot of work for my door, which has created a way to access the property, so we don't need to write again. What you need to do is how this class maps we created database tables. Active Record is responsible for the object name to the mapping of the table name. Specific See: http://api.rubyonrails.org/classes/activeRecord/base.html
Now we have to do this to the Person class: Def self.table_name () "people" End Then open the browser, open the address http: // rails / friends / than. "Rails" is a machine name, and if everything is normal, you should be able to see that the app has started working. Note The URL includes controllers Friends and action displays. Rails' Web Site http://www.rubyonrails.org/show/homepage