Windows Ruby on Rails development Environment installation configuration graphics and text tutorials _ruby topics

Source: Internet
Author: User
Tags save file zip ruby on rails

This article details how to configure Ruby on Rails development environment in Windows, and hopefully it will help ror beginners.

First, download and install Ruby

The best choice to install Ruby under Windows is Rubyinstaller (one-click installation package).
Download Address: http://rubyforge.org/frs/?group_id=167.
We are here to download the newer Rubyinstaller-1.9.3-p0.exe one-button installation package. This installation package contains a number of useful extensions (such as gems) and help documentation, in addition to the Ruby itself.

Double-click the installation, the following interface appears in the installation process. As shown in figure

Here we choose the installation path for D:\Ruby. The following 3 options are: (1) whether to install TCLTK support. (2) Add Ruby command path to System environment variable path. (3) whether to associate. RB and. rbw files to Ruby.
Here we all tick. Click on "Install" installation completed.

Open the cmd window and run ruby-v to display the current version number of Ruby. As shown in figure

You can also enter Ruby-e ' puts ' Hello World ', then show Hello World.

You can also open Notepad to write a piece of code such as: puts "Hello World", Save as TEST.RB, and then run Ruby test.rb in CMD to show the results Hello World.

I think you will not be reconciled to use Notepad to write the program, here recommend a lightweight Code editor SciTE, support a variety of syntax highlighting and highlighting, and free open source. Previous rubyinstaller with SciTE, and the new version needs to download itself. Official address: scintilla.org Windows has green version and installation version, installation version download point here.

After installing SciTE, open it, we enter code puts "Hello World", click menu =>file=>save save File as test.rb. Then press the keyboard F5 and the Output window on the right shows the results. As shown in figure:

Second, download and install RubyGems

RubyGems is a handy and powerful Ruby package manager, similar to the Redhat rpm. It packs a ruby application into a gem as an installation unit. Features: Remote installation package, the management of dependencies between packages, simple and reliable uninstall, query mechanism, can query local and remote server package information, can maintain a package of different versions, web-based viewing interface, can view your installed gem information.

From the official download RubyGems, take the zip version for example, unpack the compressed package, from the CMD prompt window into the setup.rb directory, run Ruby SETUP.RB can be installed.

The newer Ruby version already contains RubyGems, so we don't have to download the installation manually. Just enter the command in the CMD window: Gem update--system, patiently waiting for a while, the existing rubygems will be updated to the current version. As pictured.

Third, download and install Rails

From the cmd prompt window, enter the directive: Gem install rails starts installing rails.

If you do not want to install the document file, you can enter: Gem install rails--no-rdoc--no-ri

The program automatically downloads and installs rails and waits patiently. As shown in the figure:

Once the installation is complete, you can see something in the path D:\Ruby\lib\ruby\gems\1.9.1\gems, which is a rails package file that is installed in the same directory as Ruby.

At this point in the cmd prompt window input instructions: Rails-v display rails version number. As shown in figure:

Four, download and install Devkit

Devkit is a tool for compiling and using the local C + + expansion pack under the Windows platform. It is used to simulate make, GCC, and SH under the Linux platform to compile. This method currently supports only Ruby installed via Rubyinstaller.

Download Devkit:http://rubyinstaller.org/downloads

If the above address can not open, download from here: https://github.com/oneclick/rubyinstaller/downloads/

We use the newer version now Devkit-tdm-32-4.5.2-20111229-1559-sfx.exe

Installation steps:

1 Extract the download Devkit to the D:\DevKit directory.
2 Open the CMD window, enter the D:\DevKit directory, enter Ruby DK.RB init. #生成config. Yml, this checks the Ruby list that will be added devkit support and only supports Ruby installed through Rubyinstaller.
3 Enter Ruby dk.rb install #开始安装.
4 Enter Ruby DK.RB review #检查要添加DevKit支持的Ruby列表是否有误, you can skip over.
5 Enter gem install Rdiscount--platform=ruby. #这一步只是验证DevKit是否安装成功, if you can install Rdiscount successful installation Devkit, you can not do it.


The whole process is as follows:

v. Create a Rails project

Open the cmd Prompt window, enter D disk, and enter instructions: Rails new www will create the Rails project structure named www in D disk.

Then enter the WWW directory, input instructions: Rails server, start rails with the Webrick server. As shown in figure:

Open the browser to enter the address http://localhost:3000/see the Welcome page, the following figure:

Let's get rails to say Hello.

Rails is an MVC framework in which rails receives requests from browsers, interprets requests to find the right controller, and then invokes the appropriate method in the controller. Then, the control invokes the appropriate view and displays the results to the user. Rails provides a quick way to create views and controls, opening the CMD command window. Enter the directory of the WWW project just now, enter the command: Rails generate controller Say hello as shown:

Below I open the view file with the hint in the previous figure, the path is/project directory/app/views/say/hello.html.erb

Modified to the following form:

Copy Code code as follows:

<p> now time is: <%= @time%></p>

Note: Save the View file Hello.html.erb as a UTF-8 encoding format, otherwise the Chinese will appear garbled. Description is UTF-8 encoding, rather than UTF-8 +bom encoding, BOM is the beginning of the file to add a few characters to represent the standard file encoding. But only Microsoft used, X-nix, PHP, Ruby do not recognize.

Next, open the controller file, path is/project directory/APP/CONTROLLERS/SAY_CONTROLLER.RB

Amended to

Copy Code code as follows:

Class Saycontroller < Applicationcontroller
def hello
@time =time.now
End
End

Open browser access: Http://localhost:3000/say/hello, the results are shown below.

If you want to change the home page, let the first page show Hello. Just 2 steps:

1 Find/project directory/config/route.rb file, find this line # root:to => ' Welcome#index ' to remove the annotation and set it to its own controller, modified to:

Root:to => ' Say#hello '

2 Delete/project directory/public/index.html files because rails prioritizes static files in the public directory.

Again visit: http://localhost:3000, home page display Say#hello, on the content of the above picture consistent.

VI Create a Rails project that uses the MySQL database

1 Open cmd window, enter D disk, enter command: Rails new work--database Mysql, create the Rails project structure named work in D disk.

2 The Libmysql.dll library must be copied to the Bin directory of the Ruby installation directory (D:\Ruby\Bin). Otherwise, you will be prompted at the start of the server: "LIBMYSQL.dll was not found, so the application failed to start." Reinstalling the application may fix the problem. ”

Download Address: http://www.mysql.com/downloads/connector/c/

We download mysql-connector-c-noinstall-6.0.2-win32.zip here, decompression, in the Lib directory inside the Libmysql.dll is what we are looking for.

3 Modify the configuration information of the database. In the/project directory/config/database.yml files, respectively corresponding to the "development", "test", "Production" database configuration, modify them to their own configuration.

The development is the database that we want to use in our development. Be sure to keep a space at least after username: and password: after that.

4 The Rake Db:craete command is entered in the Cmd window, and the database set up in the configuration information is created in MSYQL.

5 we use scaffold to generate code, in CMD, enter the following instructions:

Rails Generate scaffold Post title:string body:string addtime:datetime

Controller, views, models, and database scripts are created, but no tables are created at this time.

Re-enter: Rake db:migrate create table. Complete.

6 Execute Command Rails server

Access through the browser: http://localhost:3000/posts This page has been generated for us to check and delete the code, OK.

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.