Rails 关于 Rails 项目的一点问题

joke1943 · 2015年10月26日 · 最后由 xiaoronglv 回复于 2015年11月04日 · 2842 次阅读

这几天一直在研究一个 ROR 项目的 DEMO,发现 ruby 真的是一门很优秀的语言,但是刚刚入手,不像 Java,php 网上的资料一搜一大片,基本上有问题了去 overstack 上搜,但是正经学习的资料很少。昨天发了一个贴子,被屏蔽了,可以是因为我发的帖子太简单了,十分抱歉。 windows 下安装失败,怎么也搭不起来 ROR 环境。所以放到了云主机上(CentOS),环境搭建 OK。一点点开始研究代码,但是项目跑不起来。很苦恼,查不到资料,所以在这里发帖求助,先在这里谢谢大家!

问题一: 这里是这个项目的 database.yml 文件

# MySQL.  Versions 5.0+ are recommended.
#
# Install the MYSQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
#
default: &default
  adapter: mysql2
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock
  # encoding: utf8
  encoding: utf8mb4
  collation: utf8mb4_unicode_ci

development:
  <<: *default
  database: iceage_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: iceage_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: iceage_production
  host: <%= ENV['DATABASE_HOST'] %>
  username: <%= ENV['DATABASE_USERNAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>

这里面 &default 与 <<: *default 是什么语法,还有 <%= ENV['DATABASE_HOST'] %>是什么语法? 这里我把

host: <%= ENV['DATABASE_HOST'] %>
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>

改为我自己的数据库。我运行 rake db:migrate RAILS_ENV=production

报错:

rake aborted!
YAML syntax error occurred while parsing /home/chenx/project-master/config/database.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Error: (<unknown>): could not find expected ':' while scanning a simple key at line 55 column 3
/home/chenx/iceage-master/config/environment.rb:5:in `<top (required)>'
Psych::SyntaxError: (<unknown>): could not find expected ':' while scanning a simple key at line 55 column 3
/home/chenx/iceage-master/config/environment.rb:5:in `<top (required)>'

我该怎么配置一下着 database 呢? 谢谢!

default: &default
  xx: xxx

<<: *default

<<: *default 其实就是继承上面的那个 default: &default 里面的内容

报错是因为你的 YAML 有 Tab,把 Tab 换成空格就好了。

#1 楼 @greatghoul 谢!那这个变量呢?

host: <%= ENV['DATABASE_HOST'] %>
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>

是类似 git 发布时候的占位符吗?

ENV 是环境变量,可以通过 rails s DATABASE_HOST=xxx.xxx.aliyun.com DATABASE_USERNAME=aaa DATABASE_PASSWORD=bbb 把对应的值传进去,图方便的话,可以直接写到 yml 里。

装个虚拟机,海阔天空。(PS: 个人用户不建议用 CentOS

#1 楼 @greatghoul #3 楼 @liwei78 谢!发现项目里有 spring,又出了问题,麻烦帮忙看一下 (https://ruby-china.org/topics/27839)

Vagrant 试试这个

需要 登录 后方可回复, 如果你还没有账号请 注册新账号