Ruby on Rails installation Configuration tutorial detailed

Source: Internet
Author: User
Tags mkdir phpmyadmin docker for mac ruby on rails

Learning and developing the Ruby on Rails application process is done on the local computer. That is, you need to create a Rails project on your local computer, run it, and then learn or do real development. Either way, you can install all the tools for the rails project directly on your local computer, and then run the Rails project directly on your local computer. In addition, we can use Docker to Compose a Rails project.

Run the Rails project directly locally

Demand

I tested the whole process on the MacOS. You need to:

Install Ruby
Install Xcode
Installing Rails

Open the terminal and use Ruby's Gem to install Rails:

Gem Install Rails
This will install the latest rails. You can also specify a specific Rails version to install, like this:

Gem Install rails-v 5.0.0.1
If you encounter an error:

Building native extensions. This is could take a while ...
Error:error Installing rails:
Error:failed to build gem native extension.
It means that you can't build a local extension, Google encountered the error, found the page: http://stackoverflow.com/questions/19580685/installing-rails-on-mavericks

The solution is to execute:

Xcode-select--install
The above command is to install Xcode command line Developer Tools (Developer command-line tool). Perform again after completion:

Gem Install Rails
The installation process takes a while, and you see the following prompts for the successful installation of rails:

Successfully installed
Create Rails Project

The Rails new command creates a new rails project that executes:

CD ~/desktop
mkdir Rails-app
CD Rails-app

Rails new.
First go to the desktop, create a Rails-app directory, enter the directory, and then create a rails project in the current directory with Rails new.

Start Rails Project

Rails server can start rails projects. Perform:

Rails Server
If you encounter an error:

Getaddrinfo:nodename nor servname provided, or not known (SOCKETERROR)
The default server's port number is 3000, if your local computer already uses 3000 of this port will encounter the above error, the solution is to create the server manually specify a new port number, like this:

Rails server-p 3300-b ' 0.0.0.0 '
So you can open the address:

http://localhost:3300
You will see the Welcome interface for Rails applications.

48aa8cb6-cedc-40b8-b2ae-b9bd7fbedf2d

Run the Rails project locally with Docker

This will be my final choice for running the Rails project locally. Later, I will also assume that you have chosen this method.

Demand

Install Docker for Mac/docker for Windows

Directory structure

→mkdir-p app Services/rails/config
→ls
App Services
→touch docker-compose.yml Services/rails/dockerfile Services/rails/config/gemfile
→tree
.
├──app
├──docker-compose.yml
└──services
└──rails
├──dockerfile
└──config
└──gemfile

4 directories, 3 files
Compose

Open the docker-compose.yml in the project root directory and define several services:

Version: ' 2 '
Services
Rails:
Build
Context:./services/rails
Dockerfile:dockerfile
Volumes:
-/app:/mnt/app
Ports
-"3,000:3,000"
Command:bundle exec Rails s-p 3000-b ' 0.0.0.0 '
Tty:true
Db:
image:mariadb:10.1
Environment:
Mysql_root_password: "ROOT"
Mysql_database: "App"
Mysql_user: "App"
Mysql_password: "123123"
Volumes:
-Db:/var/lib/mysql
phpMyAdmin:
Image:phpmyadmin/phpmyadmin
Ports
-"8,081:80"
Environment:
Pma_host: "DB"
Pma_user: "Root"
Pma_password: "Root"
Volumes:
Db:
Driver:local
Gemfile

Open the Services/rails/config/gemfile file and enter the contents:

SOURCE ' https://rubygems.org '
Gem ' rails ', ' 5.0.0.1 '
Dockerfile

Open the Services/rails/dockerfile file and enter the contents:

From ruby:2.3
Maintainer Wanghao <wanghao@ninghao.net>

RUN apt-get update && apt-get install-y nodejs \
&& rm-rf/var/lib/apt/lists/*

RUN gem Install rails--pre--no-ri--no-rdoc
RUN mkdir-p/mnt/app
Workdir/mnt/app
RUN Rails New.
Expose 3000
Start a service

Run Docker, and then in the project's root directory, execute:

Docker-compose up-d
Create a project

In the root directory of the project, execute the command to create a rails project:

Docker-compose Run rails rails new.
The new project file is stored under the app directory.

Start again

After the new project is created, execute again:

Docker-compose up-d
Then open the browser and enter the address:

http://localhost:3000
You will see the following welcome interface:

7917d846-21e0-4a4e-b784-a886334c4582

Execute command

When developing rails applications, you need a lot of command-line tools, and we can go into the Rails services container and execute the commands we need. Enter the container for the Rails service:

Docker-compose EXEC Rails Bash
Hello,rails

Let Rails say "hello" and you can create a simple controller and view.

Using the controller manufacturer, create a controller with the name Welcome, which has an index action:

Rails Generate Controller Welcome index
The result is like this:

Running via Spring Preloader in Process 54
Create App/controllers/welcome_controller.rb
Route get ' Welcome/index '
Invoke Erb
Create App/views/welcome
Create App/views/welcome/index.html.erb
Invoke Test_unit
Create Test/controllers/welcome_controller_test.rb
Invoke Helper
Create App/helpers/welcome_helper.rb
Invoke Test_unit
Invoke assets
Invoke Coffee
Create App/assets/javascripts/welcome.coffee
Invoke Scss
Create App/assets/stylesheets/welcome.scss
Controller:

App/controllers/welcome_controller.rb
View:

App/views/welcome/index.html.erb
Open Index.html.erb This view, delete all the contents inside, add the following things:

Access Address:

Http://localhost:3000/welcome/index
You will see "Hello".

Set the home page for the application

Edit file:

Config/routes.rb
The contents are as follows:

Rails.application.routes.draw do
Get ' Welcome/index '
Root ' Welcome#index '
End
Root ' welcome#index ', meaning to use the Welcome Controller's index action to process the request to the application homepage.

Get ' welcome/index ', meaning to request Welcome/index this address, use the Welcome Controller's index to handle.

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.