Steps for installing rails and generating small instances

Source: Internet
Author: User
Tags processing text

1. Ruby is a free, simple, intuitive, scalable, portable, and interpreted scripting language for fast and simple object-oriented programming. Similar to Perl, it supports many features for processing text files and executing system management tasks.

Rails is a complete open-source Web framework written in Ruby. It aims to use simpler and fewer code to write actually used applications.

 

Rubygems is a standard Ruby Package Manager, which features similar to Apt-get in Linux. You can use rubygems to conveniently download and install rails from a remote server.

 

Open the command line window, enter and execute the command gem install rails -- remote or gem install rails -- include-dependencies,

 

Note that to install rails using gem install rails--remote, you can directly download the rails installation package file from a remote server, and use the gem install rails--include-dependencies command to install rails, the system first checks whether the rails installation package exists on the local host. If not, the system downloads and installs the package from the remote server.

 

 

However, if a problem occurs when installing rails

[Root @] # gem install rails

Bulk updating gem source index for: http://gems.rubyforge.org

Error: While executing gem... (GEM: gemnotfoundexception)

Cocould not find rails (> 0) in any Repository

 

May be missing the corresponding installation package or network problems, you can go to the http://blog.sxia.net/temp/gem.rar

Download the package, perform local installation, and then update

After the installation is complete, run the rails-V command to check whether the installation is successful.

 

II.

1. Run rails mybook in the DOS command line window to create an empty Rails Web application skeleton

2. Run the mybook directory input command Ruby script/server. This command runs the server command under the script directory to start the webrick server. Access http: // 127.0.0.1: 3000

3. Open the new DOS command line and run Ruby script/generate controller mytest

D:/railsdoc/mybook/APP/controllers. A file named my_test_controller.rb containing the mytestcontroller class definition skeleton is generated.

4. Edit the mytestcontroller class

Def Index

Render_text "Hello World"

End

Def hello

Render_text "Hello rails"

End

After adding the preceding section, you can access http: // 127.0.0.1: 3000/my_test, http: // 127.0.0.1: 3000/my_test/Hello

 

Note: by default, rails needs to find an index method in the Controller class. This is easy to understand, similar to the index.html file under the Apache server directory.

When running Ruby script/generate controller mytest, rails makes some default assumptions. The Controller file corresponding to mytest is my_test_controller.rb, the Controller class name in the file is mytestcontroller, And the URL ing is/my_test /.

The advantage of these default rules is that I do not need to edit complicated configuration files, as long as the corresponding files are edited. We only need to pay attention to the program logic, rather than anything else. This is the "Convention is better than configuration" in the rails design principle ".

 

5. Install the MySQL database

After installing MySQL, find libmysql. dll and copy the file to the bin directory of the Ruby installation directory.

Go to the http://rubyforge.org/projects/mysql-win and download mysql- 2.7.3 -Mswin32.gem: mysql_ruby driver.

Command Line, enter the directory where the file is located, and run gem install mysql- 2.7.3 -Mswin32.gem: the installation is successful.

 

6. Modify the config/database. yml file, change the database name and password, and restart the server.

 

7 Ruby script/generate Model Book generate the skeleton file book. RB of the model class Book

Rails maps the books table in the database to the book class. Here, rails uses the Convention again, that is, database tables are named in the plural form, and the corresponding model class is in the singular form. Rails is very intelligent here. It understands the English plural rule and automatically maps the person class to the plural form people database table.

 

8. Ruby script/generate controller book

Generate the Controller skeleton file book_controller.rb.

Add scaffold: Book

-This is the only code you need to write. It generates the CRUD operation corresponding to the database table, and it generates the corresponding view template file.

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.