02. rails安装

rails安装

ruby 1.8.6 对应的rails是2.3.2
Ruby 1.8.7 或 1.9.2 对应的rails 2.3.x
ruby1.9.3,对应的rails是3.2.12,
ruby2.0.0,对应的rails版本是4.0 | Rails 4.2.5
ruby 2.3.8p459,对应的rails版本是5.0.7 | Rails 5.0.7
ruby2.7.3,对应的rails版本是5.0
ruby3.0.0,对应的rails版本是 6.1.1,

ruby各版本下载地址
https://rubyinstaller.org/downloads/

使用rvm 1.9.3 exec rails new project 就可以创建一个使用rails3.2.12版本的rails项目了。
参考:
https://ruby-china.org/wiki/rvm-guide

1.安装:

查看目前使用的版本

  haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby/circles$ rvm list
  =* ruby-2.3.8 [ x86_64 ]
     ruby-2.5.8 [ x86_64 ]
     ruby-3.0.0 [ x86_64 ]


  # => - current
  # =* - current && default
  #  * - default

安装最新版本为Rails 6.1.1

root@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby# rvm use 3.0.0 --default
Using /home/haima/.rvm/gems/ruby-3.0.0
root@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby# gem -v
3.2.3
root@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby# gem install rails  #不写版本号,既安装最高版本的rails ,目前最新版本为Rails 6.1.1
root@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby# rails -v
Rails 6.1.1

启动服务

rails s -p 3001

访问页面

localhost:3001

或者(根据自己的需要,这里我两个版本的都分别安装了一下,试试多版本管理 ,呵呵)

安装Rails 5.0.2

root@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby# rvm use 2.5.8 --default
Using /home/haima/.rvm/gems/ruby-2.5.8
root@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby# gem -v
3.0.9
root@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby# gem install rails -v 5.0.2   #指定安装:gem install rails -v 5.0.2
root@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby# rails -v
Rails 5.0.2

rails版本查询
https://rubygems.org/gems/rails/versions

实际操作:

  1. 创建一个项目

rails new master_rails_by_actions_hm --skip-puma --skip-turbolinks -d mysql

说明:
    --skip :为不安装的组件
    --skip-puma:跳过puma配置
    --skip-turbolinks :跳过前端组件
    -d mysql :指使用mysql
  1. 修改镜像源

    打开vi Gemfile

    a. 镜像源修改为下面的source

    source 'https://gems.ruby-china.com/'

    更多镜像源参考:
    修改Ruby的gem源(gem sources)到国内:
    https://gems.ruby-china.com/
    更多参考:
    https://blog.csdn.net/JineD/article/details/106634180?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.control

    或者全局修改
    gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

    查看修改结果

    root@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby# gem sources
    *** CURRENT SOURCES ***
    
    https://gems.ruby-china.com      
    

    b. 打开 gem 'therubyracer', platforms: :ruby的注释,

    这个为了可以让你在 Ruby 程序中嵌入 V8 引擎。

  2. 关闭generators的默认创建资源文件的行业
    修改 config/application.rb

module MasterRailsByActionsHm
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # 关闭generators的默认创建资源文件的行业
    config.generators do |generator|
      generator.assets false #创建controller时,不要生成对应的js / css
      generator.test_framework false #创建model时,不要生成test文件
    end

  end
end
  1. 修改数据库配置

config/database.yml

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: 123456
  socket: /tmp/mysql.sock
  1. 安装 Bundler

    $ gem install bundler

  2. 开始下载依赖

    bundle

  3. 创建数据库

haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby/master_rails_by_actions_hm$ rails db:create
Resolving dependencies...
Created database 'master_rails_by_actions_hm_development'
Created database 'master_rails_by_actions_hm_test'
  1. 启动rails

    rails s -p 3001 #如果不写端口,默认是3000
    rails s -b 0.0.0.0 #-b 开放ip,外网可访问

报错1

如果报下面的错:说明没有安装nodejs

[root@centos7 rails_test]# rails s
/usr/local/rvm/gems/ruby-2.3.8/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect': Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

nodejs参考下面的连接安装一下就ok了:
https://www.cnblogs.com/haima/p/11235640.html

报错2:

/home/haima/.rvm/gems/ruby-3.0.0/gems/webpacker-5.2.1/lib/webpacker/configuration.rb:99:in `rescue in load': Webpacker configuration file not found /media/haima/34E401CC64DD0E28/site/go/src/ruby/circles/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /media/haima/34E401CC64DD0E28/site/go/src/ruby/circles/config/webpacker.yml (RuntimeError)

解决方法:

rails webpacker:install
rails s
 
#或者指定端口
#rails s -p 3001 
  1. 访问页面

localhost:3000

至此安装成功

设置rails 全局的env
如果不设置默认为development
有以下几种模式
["default", "development", "test", "production"]

临时生效:
export RAILS_ENV=production
export RAILS_ENV=development

haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/ruby/fofapro$ echo $RAILS_ENV
development

永久方法:

vim /etc/profile
写入
export RAILS_ENV=production

保存退出

更新权限 source profile

rails c 进入rails控制台

指定环境变量启动项目

生成密钥

rake secret RAILS_ENV=production

启动系统

rails s -e production

启动控制台

rails c -e production

Rails实战之B2C商城开发源码:
学习视频:
https://www.bilibili.com/video/BV1uJ411s7su?p=2
代码:
https://gitee.com/haima1004/master_rails_by_actions

posted @ 2021-01-08 09:24  HaimaBlog  阅读(334)  评论(0编辑  收藏  举报