# 环境构建

# 首先搭建好环境

可参考如下教程

# 检测环境是否搭建成功

  • ruby -v 检查版本
  • rails -v

# 新建一个 app 项目

  • rails new app -d mysql

# 启动 rails 服务器

# 普通模式启动
  rails s
# 启动手机监听
  rails s -b 0.0.0.0

# 安装 Rails 可能遇到的问题

# 方法一:将 https 修改为 http
  $ gem sources --add http://rubygems.org/

# 方法二:使用淘宝镜像源(https://ruby.taobao.org/)
  $ gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
  $ gem sources -l
  *** CURRENT SOURCES ***
  https://ruby.taobao.org
  # 请确保只有 ruby.taobao.org
  $ gem install rails

# 方法三:使用 Ruby-China 镜像源(https://gems.ruby-china.org/)
  $ gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
  $ gem sources -l
  https://gems.ruby-china.org
  # 确保只有 gems.ruby-china.org

# Rake 版本问题

如果有用 Bundler 的話,你可以在 Gemfile 中指定 gem 'rake', '0.8.7',然後每次打 bundle exec rake 就是用 0.8.7 的版本了。BTW,如果指定了 0.8.7 還打 rake 的話,你會看到以下錯誤:

rake aborted!
You have already activated rake 0.9.0,
but your Gemfile requires rake 0.8.7.
Consider using bundle exec.
(See full trace by running task with --trace)

解决方法:

  • 方法一:命令前添加 bundle exec,例如:bundle exec rake db:migrate
  • 方法二:卸载新版本的而插件,参考 「gem 插件版本不对」

# gem 插件版本不对

特别是提示 当前 rake 版本高于需要使用的 rake 版本

# 查看所有插件
gem list

# 卸载插件
gem uninstall <插件名>

# 选择后卸载

# Ignoring — because its extensions are not built. Try: gem pristine

然后执行:rvm install ruby-2.2.3

# mysql 问题

  #`Can't connect to local MySQL server through socket  /tmp/mysql.sock`
  $ mysql.server start

  #MySQL2 Error - Unknown database
  rake db:create
Last Updated: 5/14/2022, 11:38:45 AM