Ruby on Rails 2.0.2 Hello world for Dummies

Source: Internet
Author: User
Tags ruby on rails website

No exception. Like all other open-source projects, if you want to enter the Ruby on Rails world, you will find very difficult, rarely documented, and scattered,
The downloaded file lacks basic instructions. My personal experience is a typical example. If you don't talk nonsense, let's start with the following:
1. installation environment <Windows>
Ruby on Rails Website: http://www.rubyonrails.org/
1. Download it here Ruby 1.8.6 Http://rubyforge.org/frs? Group_id = 167
It already contains rubygems 0.9.4 and does not need to be downloaded. Alternatively, you can upgrade it to version 1.1.1.
2. If your network is fast enough, run the following command to download Program Automatic package installation
Gem install rails -- include-dependencies
Otherwise, download stand-alone packages for manual installation, which contains several files:
Rails-2.0.2.zip http://rubyforge.org/frs/download.php/29361/rails-2.0.2.zip
Rails-2.0.2.gem http://rubyforge.org/frs/download.php/29554/rails-2.0.2.gem
Available online Article Introduction: Place rails-2.0.2.zip In the ruby installation directory. Run the gem install rails command to automatically decompress and install it. Haha, I tried it many times. It seems that this method does not work at all,
The original author is also vague, so we will not go into details. After all, running rails requires the following dependency packages before you can install rails.
Activesupport
Activerecord
Actionpack
Actionmailer
Activeresource
These dependency packages are included in the vendor \ rails directory in the rails-2.0.2.zip file. The corresponding gem files are displayed in the PKG subdirectory of the corresponding directory in sequence. you can install these files. For example

C: \ Rails \ Vendor \ Rails \ Activesupport \ PKG > Dir
The volume in drive C is not labeled.
The serial number of the volume is 3754 - 1bdb

C: \ Rails \ Vendor \ Rails \ Activesupport \ PKG directory

2008 - 04 - 22 17 : 51 < Dir > .
2008 - 04 - 22 17 : 51 < Dir > ..
2007 - 12 - 16 19 : 05 < Dir > Activesupport - 2.0 . 2
2007 - 12 - 16 19 : 05 234 , 496 Activesupport - 2.0 . 2 . Gem
2007 - 12 - 16 19 : 05 233 , 039 Activesupport - 2.0 . 2 . Tgz
2007 - 12 - 16 19 : 05 285 , 376 Activesupport - 2.0 . 2 . Zip
3 Files 752 , 911 Bytes
3 Directories 6 , 119 , 768 , 064 Available bytes

C: \ Rails \ Vendor \ Rails \ Activesupport \ PKG > Gem install activesupport

So manual installation only need to install the bread, and finally install the rails-2.0.2.gem can be.
After the installation is complete, run rails-V to display the rails 2.0.2 version.

Ii. Starting from Hello World
To tell the truth, who in the first Rails Web program wants an example of connecting to MySQL or other databases? At least I don't want this, however, the first example of a popular online entry is to connect to a database to demonstrate what you do not need to write.CodeThe example of adding, deleting, and modifying data can be achieved. Although it is so dazzling, it is too troublesome for me to wait for the publisher. Hello world is what we hope.
(Note: many popular e-books and tutorials are also examples of such databases. For details, refer to rails cookbook, rails for Java developers, and rails recipes.
Tutorials officially introduced: http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html
Rails 2.0 Getting Started Guide http://www.lupaworld.com/action_viewstutorial_itemid_10205.html)
There is also a problem: some things of 2.0 have changed compared with 1.x, so many tutorials on the Internet are based on 1.x, and many people have to make a detour!
1. Create a web application
Run rails c: \ Ruby \ projects \ sample to create a required sample application. This operation creates some basic directory frameworks and necessary files. After the application is created, you can enter the directory to check the effect.

2. Start the Web server and test the effect.
Enter the c: \ Ruby \ projects \ sample directory, run the ruby script/server to start the server, and then open the URL http: // localhost: 3000/to view the initial Page.

3. Prepare sqlliteSorry, we don't want to involve databases. We can still encounter this problem in config \ database. you can see its shadow in the yml file. No way. This must be installed; otherwise, an error will occur later.
Download http://rubyforge.org/frs/ here? Group_id = 254 file sqlite3-ruby-1.2.1-mswin32.gem, run the gem install sqlite3-ruby installation.
You must also download the sqllite3.dll file, it is really troublesome, you can download the http://www.sqlite.org/download.html download file sqlitedll-3_5_8.zip from here, decompress it to the bin directory of Ruby, otherwise the error of sqlite3.dll file not found will be reported later.

4. Because rails uses the MVC Architecture to organize web programs, you can run script/generate to generate the required files. This script has built-in controller, Mailer, model, scaffold, and web_service programs, here we need a controller, and other generator plug-ins can be found online, such as automatically generating a logon interface.
Go to the C: \ Ruby \ projects \ sample directory and runRuby Script/generate controller welcome Hello
Similar to the following output, here welcome specifies the control name. In the file welcome_controller.rb, a class welcomecontroller is generated, which is similar to the action in Java. One of the methods is hello.

C: \ Ruby \ Project \ Sample > Ruby script / Generate controller welcome Hello
Exists app / Controllers /
Exists app / Helpers /
Create app / Views / Welcome
Exists Test / Functional /
Create app / Controllers / Welcome_controller.rb
Create Test / Functional / Welcome_controller_test.rb
Create app / Helpers / Welcome_helper.rb
Create app / Views / Welcome / Hello.html. ERB

Add the following code to the app \ controllers \ welcome_controller.rb File 1 Class Welcomecontroller < Applicationcontroller
2
3 Def Hello
4 @ Welcome_message =   "Welcome to your first rails Application "
5 End
6 End
7

APP \ views \ Welcome \ hello.html. ERB file: 1 <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en" >
2 < Html >
3   < Head >
4 < Title > Hello World </ Title >
5   </ Head >
6
7   < Body >
8 < H1 > Welcome # Hello, <% = @ Welcome_message %> </ H1 >
9 < P > Find me in APP/views/welcome/hello.html. ERB </ P >
10   </ Body >
11 </ Html >
12

As a result, start the Web server and enter the URL http: // localhost: 3000/welcome/hello to see the effect.

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.