Ruby on Rails website project construction simple guide, rubyrails

Source: Internet
Author: User
Tags ruby on rails website

Ruby on Rails website project construction simple guide, rubyrails

Create a Rails Project

To create a common Rails project, run the following command:

rails new blog

However, in China, because it is too slow to connect to RubyGems, Rails downloads and installs dependency packages from RubyGems by default after the project structure is built. In the end, it will be stuck due to network problems. Therefore, you must use the -- skip-bundle parameter to skip the bundle execution step. Then, use the Gems image source in China to install the dependent package. The recommended source in China is Ruby China: https://gems.ruby-china.org /.

The final steps are as follows:

Create a project:

rails new blog --skip-bundle

Modify the Gemfile file in the project directory to change the value of the content following the source quotation marks in the first line to: https://gems.ruby-china.org/
Finally, execute bundle install in the project directory to install the dependency package.
So far, a standard Rails Project Skeleton has been created.

Configure a Rails Project

By default, you can use the generated project skeleton to start project development. However, you still need to adjust some configurations as needed. For example, the time zone or MySQL.

Set Time Zone

By default, Rails uses the UTC time zone. If the project is only used in China, the data and time-related values will be 8 hours later than the domestic time. Therefore, for non-international projects, you can set the time zone to China.

Open the config/application. rb file in the project directory. Remove the comments before the config. time_zone configuration item, modify the value of the configuration item to Beijing, and add a new configuration line:

config.active_record.default_timezone = :local

Use MySQL

By default, Rails uses the sqlite database. To use MySQL, you also need to install the MySQL package. And modify the default database configuration.

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

gem 'mysql2'

Run bundle install to install the dependency package.

Next, open the config/database. yml file and modify the following format for the configuration of the development Block:

development: adapter: mysql2 database: blog username: root password: host: 127.0.0.1

Create a Rails document

This step is not necessary for veterans who have mastered Rails. However, new users like me often need to turn over the manual for help and move the document to the local environment, saving the trouble of no network or slow network speed. Rails provides commands to generate local Rails manuals and API documents in projects.

First, add a dependency package: redcarpet. Open the Gemfile file in the project. Because it is only used in the local environment, you do not need to add it to the global dependency. Find the development Configuration block at the bottom of the file and add it here. The format is as follows:

gem 'redcarpet', '~> 3.1.2'

Then run: bundle install installation package.

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

rake doc:guidesrake doc:rails

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

Create a custom command in Rails
When developing Web applications, you will inevitably encounter command scripts that need to be written on the terminal. For example, you need to import data from a large text. It is quite simple to do this in Rails. The following is an example.

First, create a script file. Create a hello. rake file in the lib/tasks directory of the Rails project. The file content is as follows:

namespace :hello do
 task :world do
  puts "Hello, World!"
 end
end

In this way, a simple greeting script is completed. To run this script, run the rake command:

rake hello:world

By default, the script command does not introduce the Rails project module. If you want to introduce it, it's easy! For example, I want to say hello to all the customers in the project:

namespace :hello do

 # ...

 task :customers => :environment do
  for customer in Customer.all
    puts sprintf("hello, %s", customer.name)
  end
 end
end

If your project contains the Customer module and the name field, you can run rake hello: MERs to verify the effect. Compared with the first task, the =>: environment Code is added here, which implements the function of introducing project environment dependencies. Is it easy?


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.