Ruby on Rails Web site project build simple guide _ruby topics

Source: Internet
Author: User
Tags documentation pack set time sqlite database ruby on rails

Create Rails Project

To create a normal Rails project, you can use the following command directly:

Rails New Blog

But in the country because the speed of connection RubyGems is too slow, and Rails by default after building the project structure, will use the bundle command to download the installation dependency package from RubyGems. Eventually it will die because of network problems. So you need to skip the step of performing bundle with the--skip-bundle parameter. Then use the domestic Gems mirror source to complete the subsequent installation of the dependent package. The source of the domestic recommendation is Ruby China offers: https://gems.ruby-china.org/.

The final finishing steps are as follows:

To create a project:

Rails New Blog--skip-bundle

Modify the Gemfile file in the project directory, and modify the value in quotation marks after source in the first line: https://gems.ruby-china.org/
Finally, execute in the project directory: Bundle install completes the installation dependency pack.
At this point, a standard Rails project skeleton is created.

Configuring Rails Projects

By default, the build project skeleton can already be used to start development projects, but some configurations need to be adjusted depending on the actual requirements. such as time zone, or use MySQL.

Set time zone

The default Rails uses the UTC time zone and if the project is only used domestically, this results in a data-related value of 8 hours later than the domestic time. So if you are not an international project, you can fix the time zone to the country.

Open the Config/application.rb file in the project directory. Remove the comment before the Config.time_zone configuration entry, and then modify the value of the configuration key to Beijing and add a new row of configuration:

Config.active_record.default_timezone =: Local

Using MySQL

The default Rails uses the SQLite database, and if you want to use MySQL, you also need to install a MySQL package. and modify the database default configuration.

Open the Gemfile file in the project directory and add a global dependency pack mysql2, in the following format:

Gem ' Mysql2 '

Then execute: Bundle Install install Dependency Pack.

Next, open the Config/database.yml file and modify the configuration of the development block in the following format:

Development:
 adapter:mysql2
 database:blog
 username:root
 Password:
 host:127.0.0.1

Create a Rails document

This step is not necessary for a veteran who has mastered Rails. But for beginners like me, it's often necessary to go through the manual to get help and move the document locally, eliminating the hassle of not having a network or a slow internet connection. Rails provides a command to generate a local manual and API documentation for rails in your project.

First you need to add a dependency pack: Redcarpet. Open the Gemfile file in the project, because it is only used in the local environment, so do not add to the global dependencies, find the development configuration block at the bottom of the file, add it here, as follows:

Gem ' Redcarpet ', ' ~> 3.1.2 '

Then execute: Bundle install installation package.

Use the following command to generate the Rails help documents and API documentation:

Rake Doc:guides
Rake doc:rails

When the command is complete, the document is generated to the doc directory.

Create a custom command in Rails
when you develop a WEB application, you will inevitably encounter a command script that needs to be written at the end of the execution. For example, to import data from a very large text. It's fairly straightforward to do this in Rails, and here's an example.

The first is to create a script file. Create a Hello.rake file in the Lib/tasks directory of the Rails project. The contents of the document are as follows:

Namespace:hello do
 Task:world do
  puts "Hello, world!"
 End End

This completes a simple greeting script, and to run this script, you need to use the rake command:

Rake Hello:world

By default, the script command does not introduce Rails's project modules. If you need to introduce is also very simple! For example, I want to say hello to all the clients in the project:

Namespace:hello do

 #

 ... Task:customers =>: Environment do to
  customer in Customer.all
    puts sprintf ("Hello,%s", Customer.name)
  End
 End


If you have a Customer module in your project and you just have the Name field, you can perform a rake hello:customers to verify the effect. In contrast to the first task, we added =>: Environment This code, it is the implementation of the introduction of the project environment depends on the function. Is it simple?

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.