[关闭]
@torresdyl 2018-03-08T18:06:51.000000Z 字数 10587 阅读 1562

Deployment APP for Ruby on Rails in Centos 7

0. Check ruby and rail version

  1. yum install epel-release
  2. wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  3. yum install ./epel-release-latest-7.noarch.rpm
  4. ruby --version
  5. gem list rails

If no rails is installed(no local gem), you should do:

  1. rvm install 2.4.2
  2. gem update --system
  3. gem install rails bundler

If not working, you must install rvm:

  1. \curl https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer | bash -s stable

Here is result:

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24090 100 24090 0 0 75503 0 --:--:-- --:--:-- --:--:-- 75754
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: Signature made Sun 10 Sep 2017 10:59:21 PM CEST using RSA key ID BF04FF17
gpg: Good signature from "Michal Papis (RVM signing) "
gpg: aka "Michal Papis "
gpg: aka "[jpeg image of size 5015]"
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
Primary key fingerprint: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Subkey fingerprint: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'
Creating group 'rvm'

Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  • First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with umask u=rwx,g=rwx,o=rx.

  • To start using RVM you need to run source /etc/profile.d/rvm.sh
    in all your open shell windows, in rare cases you need to reopen all shell windows.

So, you can add the user to rvm with:

  1. usermod -a -G rvm <user_name>

Check the group you belong to with:

  1. groups

and log out-log in.

At last, use:

  1. source /etc/profile.d/rvm.sh

and you are done.

Check with rvm, now available.
Run these again:

  1. rvm install 2.4.2
  2. gem update --system
  3. gem install rails bundler

and we can list rails again, we can see something like:

  1. *** LOCAL GEMS ***
  2. rails (5.1.4)
  3. rails-dom-testing (2.0.3)
  4. rails-html-sanitizer (1.0.3)
  5. sprockets-rails (3.2.1)

1. Start a new app

  1. cd /var/www/html/
  2. mkdir RoR_demo
  3. cd RoR_demo
  4. rails new demo

(don't run it as root. )
Then a dir demo will be created, with all ruby-on-rails files.

2. Change ruby version

Then, cd into it, and write the ruby version you want to use in .ruby-version.

  1. echo "2.4.2" > .ruby-version

3. Add group in Gemfile

Edit Gemfile to add these lines:(add gem pg, PostgreSQL, and gem unicorn, the Rack HTTP Service)

  1. group :production do
  2. gem 'pg'
  3. gem 'unicorn'
  4. end

If you like, you can also use the unicorn server during development by declaring the dependency outside of the :production group.

You could optionally wrap the sqlite3 gem in a group for development and test (if you use it to test on your development machine) so that it doesn’t get installed in production:

  1. group :development, :test do
  2. gem 'sqlite3'
  3. end

Then, install all dependencies locally to update Gemfile.lock:

The parameter --without=production is only needed when bundle install is run the first time, after that Bundler remembers the setting in .bundle/config.

  1. bundle install --without=production

Every time we edit Gemfile, we must do bundle install to update.

4. Install basic pages

You can generate a controller for index page and a model

  1. bin/rails generate controller welcome index
  2. bin/rails generate model counter name:string value:integer

If you fail with error Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
, you have not installed NodeJS yet. Install by:

  1. yum install nodejs

5. Change website root

Edit config/routes.rb and set the welcome controller as root route:

  1. Rails.application.routes.draw do
  2. root 'welcome#index'
  3. # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  4. end

Edit app/controllers/welcome_controller.rb and make it use the model object:

  1. class WelcomeController < ApplicationController
  2. def index
  3. counter = Counter.find_or_create_by(name: "default")
  4. counter.value = (counter.value || 0) + 1
  5. counter.save!
  6. render plain: "Counter #{counter.value}; #{Rails.version}/#{RUBY_VERSION}"
  7. end
  8. end

6. Migrate the database and run

  1. bin/rake db:migrate
  2. bin/rails s

It runs on localhost:3000. Try with explorer with IP.

7. Install Mina and setup

8. Install PostgreSQL

  1. sudo yum install postgresql postgresql-server postgresql-contrib

9. start server

  1. rails server

If no controller is defined, error. We must define controller in:

  1. <project_root>/app/controllers/app/application_controller.rb

Like:

  1. class ApplicationController < ActionController::Base
  2. protect_from_forgery with: :exception
  3. def hello
  4. render html: "Hello World!"
  5. end
  6. end

and then change <project_root>/config/routes.rb:

  1. Rails.application.routes.draw do
  2. resources :users
  3. root 'application#hello'
  4. # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  5. end

And then go back to project root, run rails server, we can see welcome page.

10. Generate scaffold

scaffold is like a basic frame of the app, including the necessary files. May be too many to digest, but worth trying.
Go back to project root(where you see Gemfile), and type:

  1. rails generate scaffold User name:string email:string

If it fails, it is because the environment variable XDG_RUNTIME_DIR is set wrongly. Change it by:

  1. export XDG_RUNTIME_DIR=/run/user/1000

(check your user id by id, 1000 is mine.)

11. Migrate with Rake(create table, update model, etc.)

  1. # for legacy problem, we may use Rake (Make of Rails)
  2. bundle exec rake db:migrate
  3. # Or, after Rails 5, we can use:
  4. rails db:migrate

And then, run the server with:

  1. rails server -b 0.0.0.0 -p 8000 # run server at some IP on some port

12. Render static pages

In the config/routers.rb, we write:

  1. root "application#homepage"

In the controller application_controller, we put:

  1. render file: Rails.public_path.join("templates", "homepage.html"), layout:true # /public/templates/homepage.html

So the file under <project_root>/public/template/ will be read.

13. Render dynamic pages

HTTP Request URL action Purpose
GET /users list of users list all users
GET /users/1 show show user with id 1
POST /users/new new add new user
GET /users/1/edit edit page to edit user 1
PATCH /users/1 update update user with id 1
DELETE /users/1 destroy delete user with id 1

14. Rails console

Enter:

  1. rails console

Consult:

  1. first_user = User.first
  2. micropost = Micropost.first
  3. posts = first_user.microposts

exit or `Ctrl

15. Generate static pages

Controller is CamelCase, while files are usually snakeCase.
Undo changes: rails destroy. Eliminate all generate files and additional changes to files like routes.rb.

  1. rails generate controller StaticPages home help
  2. rails destroy controller StaticPages home help # can cancel the previous

Or,

  1. rails generate model User name:string email:string
  2. rails destroy model User

We can also undo migrations.

  1. rails db:migrate
  2. rails db:rollback

Or, to go back to the beginning:

  1. rails db:migrate VERSION=0

To check migration version/history, we have:

  1. rails/rake db:migrate:status

or,

  1. rails console
  2. ActiveRecord::Migrator.current_version
  3. ActiveRecord::Migrator.get_all_versions

16. Shortcuts

Full command Shortcut
rails server rails s
rails console rails c
rails generate rails g
rails test rails t
bundle install bundle

17. Testing

Ruby on Rails uses TDD. Test code can be generated when generating the controller/model...
test/controllers/static_pages_controller_test.rb:

  1. test "should get about" do
  2. get static_pages_about_url
  3. assert_response :success
  4. assert_select "title", "About"
  5. end

In app/test/controller/static_page_controller_test.rb, we can see:

  1. require 'test_helper'
  2. class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  3. test "should get home" do
  4. get static_pages_home_url
  5. assert_response :success
  6. end
  7. test "should get help" do
  8. get static_pages_help_url
  9. assert_response :success
  10. end
  11. end

At last, when we run test, if we select something not used in the html, it reports failure too.

Variable in test:

  1. require 'test_helper'
  2. class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  3. def setup
  4. @base_title = "Ruby on Rails Tutorial Sample App"
  5. end
  6. test "should get home" do
  7. get static_pages_home_url
  8. assert_response :success
  9. assert_select "title", "Home | #{@base_title}"
  10. end
  11. test "should get help" do
  12. get static_pages_help_url
  13. assert_response :success
  14. assert_select "title", "Help | #{@base_title}"
  15. end
  16. test "should get about" do
  17. get static_pages_about_url
  18. assert_response :success
  19. assert_select "title", "About | #{@base_title}"
  20. end
  21. end

17. Embedded page (.erb)

We can execute code in page and insert them with <% %> and <%= %>.

  1. <% provide(:title, "Home) %>
  2. ...
  3. <%= yield(:title) %>

18. Helper methods

19. Learn Ruby with Rails console

  1. rails console
  2. Loading development environment
  3. >>

In Ruby we have 3 environments: development, test and production. Now the difference is trivial, but we will see more.

#{a}: interpolation. Will not interpolate ' ' strings.

  1. 2.4.2 :021 > a = 'a'
  2. => "a"
  3. 2.4.2 :022 > b = 'b'
  4. => "b"
  5. 2.4.2 :023 > '#{a} b'
  6. => "\#{a} b"
  7. 2.4.2 :024 > "#{a} b"
  8. => "a b"

The use of ' ': auto-escape.

  1. 2.4.2 :030 > print "\n"
  2. => nil # interprete it
  3. 2.4.2 :031 > print '\n'
  4. \n => nil # escape and print the literal
  5. 2.4.2 :038 > puts "#{city}\t#{state}"
  6. Madrid Madrid
  7. => nil
  8. 2.4.2 :039 > puts '#{city}\t#{state}'
  9. #{city}\t#{state}
  10. => nil

puts prints and new line; print no new line.

object in Ruby:

  1. >> "foobar".empty?
  2. => false
  3. >> {}.empty?
  4. => true
  5. >> [].empty?
  6. => true

flow control:

  1. >> if s.nil?
  2. >> "It is nil"
  3. >> elsif s.empty?
  4. >> "It is empty"
  5. >> elsif s.include?("foo")
  6. >> "Included"
  7. >> end

&& (“and”), || (“or”), and ! (“not”)

  1. >> puts "Both strings are empty" if x.empty? && y.empty?

We can use unless above in place of if.

Ternary: <condition> ? <action1> : <action2>

nil.to_s
=> ""

object.nil?
=> true

nil is the only object in boolean is false.

  1. >> !!nil
  2. => false
  3. >> !!0
  4. => true # 0 is true
  5. >> s = 'racecar'
  6. >> s.reverse
  7. => "rececar"
  8. >> s == s.reverse
  9. => true

Ruby has an implicit return: if no return, return the last statement evaluated. Or, with return, which can be like this:

  1. def method()
  2. return "A" if str.empty?
  3. return "B"
  4. end

To include a module in Ruby, we use include. Helper modules are included by default, needs no extra work.

split: split string with space, one or more.

  1. >> "foo bar foobar".split
  2. => ["foo", "bar", "foobar"]

get array element: a[0], a.first, a.last, a.second/third/fourth/fifth(a[1~4])

You can call empty?, include?(obj), sort(!), reverse(!), shuffle(!). ! means change the array itself, without ! is copying.

Add element: push(obj), <<, or append. Can be chained.

Ruby arrays can contain different type of things.

  1. >> a = ["foo"]
  2. => ["foo"]
  3. >> a << 7
  4. => ["foo", 7]

We can join array to convert it to string.

  1. >> a = ["foo", 7]
  2. => ["foo", 7]
  3. >> a.join
  4. => "foo7" # join to string
  5. >> a.join(', ')
  6. => "foo, 7"

range: first inclusive. ..: last inclusive, ...: last not inclusive.

  1. >> (0..9).to_a
  2. => [0, 1, 2, ..., 9]
  3. >> (0...9).to_a
  4. => [0, 1, 2, ..., 8]
  1. >> a = %w[foo bar foobar] # generate string array
  2. => ["foo", "bar", "foobar"]
  3. >> a[0..1]
  4. => ["foo", "bar"]
  5. >> a[1, -1] # select to the last, without using length.
  6. => (original array)

form a natural letter array

  1. >> a = ('a'..'z').to_a
  2. => ["a", "b", "c", ..."z"]
  3. >> a[-26]
  4. => "a"
  5. >> a[-27]
  6. => nil # cannot find element more than length

20. blocks

Short block: {|var| ...}.
Long block:

  1. <method> do |var|
  2. ...
  3. ...
  4. end

Note that the |var| part is in the block, not out of it.

For example:

  1. >> (1..5).each { |i| puts 2*i }
  2. 2
  3. 4
  4. 6
  5. 8
  6. 10
  7. => 1..5
  1. >> (1..5).each do |i|
  2. ?> puts 2*i
  3. >> end
  4. 2
  5. 4
  6. 6
  7. 8
  8. 10
  9. => 1..5 # returns the range

|var| represents the variable iterated.

map creates a new array to return. each returns the original array. Compare:

  1. >> (1..5).map {|i| i**2}
  2. => [1, 4, 9, 16, 25] #returns an array

Or we can write:

  1. >> ('a'..'Z').map(&:downcase)
  2. => ["a", "b", "c", "d", ..."z"]

The best example is this:

  1. def yeller(array)
  2. a = array.each(&:upcase)
  3. puts a
  4. puts array
  5. puts "a: " + a.join
  6. puts "array: " + array.join
  7. puts "array == a:" + array==a?
  8. end
  9. yeller(%w[a, b, c])
  10. def yeller(array)
  11. a = array.map(&:upcase)
  12. puts a
  13. puts array
  14. puts "a: " + a.join
  15. puts "array: " + array.join
  16. end
  17. yeller(%w[a, b, c])

Result:

  1. a,
  2. b,
  3. c
  4. a,
  5. b,
  6. c
  7. a: a,b,c
  8. array: a,b,c
  9. A,
  10. B,
  11. C
  12. a,
  13. b,
  14. c
  15. a: A,B,C
  16. array: a,b,c
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注