新手问题 (菜鸟提问) 如何在新的项目里运行 rails 服务器?

xiaozhao · 2015年09月02日 · 最后由 chenge 回复于 2015年09月06日 · 3402 次阅读

本人超级菜鸟,刚学完用 rails 建立新博客,朋友给一个 GIT 里的项目做练手,下载到本地以后不能直接启动 rails s,不知道怎么办?请前辈指点, 朋友让看 rails 教程 来自己学习设置:给的方法

1 设置好 config/database.yml 2 rake db :setup 3 rails s

rails 教程http://guides.ruby-china.org/getting_started.html#hello,-rails-bang 基础学了一遍,没有找到设置 database.yml 的方法, 谷歌上不去,百度搜索也找了两天了,没有找到如何配置 在本站的 新手问题里也扫了 N 页没找到,新手也不知道应该找哪些关键字,搜索本站但一直在转转,不显示内容,

做了各种尝试了,实在不知道怎么办了,第一次提问,也不知道如何描述,请多谅解!希望能提示一些关键字!万分感谢

项目里有两上这个文件:database.yml.mysql 和 database.yml.sqlite3 database.yml.mysql

development: &development adapter: mysql2 host: localhost username: root password: database: your_local_database

test: &test adapter: mysql2 host: localhost username: root password: database: your_test_database

production: &production adapter: mysql2 host: localhost database: your_production_database username: your_production_database_login password: your_production_database_password

第二个是: database.yml.sqlite3

SQLite version 3.x

development: adapter: <%= "jdbc" if defined?(JRUBY_PLATFORM) %>sqlite3 database: db/development.sqlite3 timeout: 5000

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: adapter: <%= "jdbc" if defined?(JRUBY_PLATFORM) %>sqlite3 database: db/test.sqlite3 timeout: 5000

production: adapter: <%= "jdbc" if defined?(JRUBY_PLATFORM) %>sqlite3 database: db/production.sqlite3 timeout: 5000

你的目标 database 是 mysql 还是 sqlite?他给了两个配置文件,分别对应 mysql 和 sqlite。 如果你用 mysql,就把 database.yml.mysql 重命名为 database.yml,放到 config 目录下面

development: &development
adapter: mysql2
host: localhost
username: root
password:
database: your_local_database

修改这部分的配置跟你 mysql 数据库配置一致就可以了

如果是用 sqlite,直接把 database.yml.sqlite 重命名为 database.yml,放到 config 目录下面,然后 db:setup 就可以了

如果还有问题,就把错误信息贴出来看看

rails new -h 中给出了提示:

-d, [--database=DATABASE]                              # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)

所以,创建项目的时候,可以

rails new project -d mysql
rails new project -d sqlite3

#1 楼 @alucardpj 万分感谢! :plus1:

#1 楼 @alucardpj 你好,我按教的方法,直接把 database.yml.sqlite 重命名为 database.yml,放到 config 目录下面,然后 db:setup 现在报错


hao@zhao:~/app/ryeboy$ rake db :setup rake aborted! LoadError: libmysqlclient.so.18: cannot open shared object file: No such file or directory - /home/zhao/.rvm/gems/ruby-2.0.0-p643/gems/mysql2-0.3.16/lib/mysql2/mysql2.so /home/zhao/.rvm/gems/ruby-2.0.0-p643/gems/mysql2-0.3.16/lib/mysql2.rb:8:in require' /home/zhao/.rvm/gems/ruby-2.0.0-p643/gems/mysql2-0.3.16/lib/mysql2.rb:8:in' /home/zhao/.rvm/gems/ruby-2.0.0-p643/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:in require' /home/zhao/.rvm/gems/ruby-2.0.0-p643/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:inblock (2 levels) in require' /home/zhao/.rvm/gems/ruby-2.0.0-p643/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:in each' /home/zhao/.rvm/gems/ruby-2.0.0-p643/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:inblock in require' /home/zhao/.rvm/gems/ruby-2.0.0-p643/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:in each' /home/zhao/.rvm/gems/ruby-2.0.0-p643/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:inrequire' /home/zhao/.rvm/gems/ruby-2.0.0-p643/gems/bundler-1.10.6/lib/bundler.rb:134:in require' /home/zhao/app/ryeboy/config/application.rb:13:in' /home/zhao/app/ryeboy/Rakefile:5:in require' /home/zhao/app/ryeboy/Rakefile:5:in' /home/zhao/.rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:in eval' /home/zhao/.rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:in' (See full trace by running task with --trace)

报错好像路径不对,而且还是直接去找的 mysql2 不知道怎么办?另外是不是学习这些最好是翻墙用谷歌?谢谢

db:setup,中间不能空格吧。

  1. 你本地安装了 mysql2 gem 了没?
  2. 你 Gemfile 里面有 gem 'mysql2'没有?
  3. 你做了 bundle install 了没?

#6 楼 @alucardpj 谢谢,后来安装了 1。mysql Ver 14.14 Distrib 5.5.44, for debian-linux-gnu (x86_64) using readline 6.3(测试了,可以进数据库) 2。Gemfile 里面有 gem 'mysql2' 。 显示: source 'https://rubygems.org' ruby '2.0.0' gem 'rails', '3.2.21' gem 'mysql2'

3。做了 bundle install。 4。改了 database.yml 显示: development: adapter: mysql2 encoding: utf8 database: ryeboy_dev pool: 5 username: root password: zhaoyan min_messages: warning host: localhost

5. rake db :setup 6。还是显示错误: rake aborted! NoMethodError: undefined method `declare' for Sass::Script::Functions:Module 7。在 config.ru 里增加了:require 'sass' 8,然后还是不行,还是上面的错误,,,菜鸟晕死了,,, 求大神!!!

建议找一些简单的项目先学习吧,循序渐进。

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