Rails Rails 项目的加上 Spring 的一点问题

joke1943 · 2015年10月27日 · 最后由 joke1943 回复于 2015年12月02日 · 3076 次阅读

这几天研究一个 ROR 项目的 DEMO,从搭建环境到运行代码,遇到了许多问题,查资料,发帖,像是闯关,一步步到了现在。临近成功,在 rake 数据迁移时遇到了一个问题,卡在这里很长时候了。感谢大家对我帖子的回复!帮助了我许多!下面我再把我遇到的问题描述一下,现在这里感谢大家了!

问题:

仔细看了一下,才发现这个项目里有 Spring,一直以为 spring 都是 Java 的 framework 框架,看到 ruby 里也在使用。这里是 spring ruby 的 github(https://github.com/rails/spring) 这里是关于 spring rake 的部分的描述

rake

Runs a rake task. Rake tasks run in the development environment by default. You can change this on the fly by using the RAILS_ENV environment variable. The environment is also configurable with the Spring::Commands::Rake.environment_matchers hash. This has sensible defaults, but if you need to match a specific task to a specific environment, you'd do it like this:

Spring::Commands::Rake.environment_matchers["perf_test"] = "test"
Spring::Commands::Rake.environment_matchers[/^perf/]     = "test"

To change the environment when you run rake with no arguments

Spring::Commands::Rake.environment_matchers[:default] = "development"

然后根据描述,运行命令

`` rake db:migrate RAILS_ENV=production ``

报错了,内容如下:

/home/chenx/iceage-master/config/initializers/load_regions.rb:1:in read': No such file or directory @ rb_sysopen - config/regions.yml (Errno::ENOENT) from /home/chenx/iceage-master/config/initializers/load_regions.rb:1:in' from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in load' from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:inblock in load' . . . . . . .

大概意思是说 ``load_regions.rb ``找不到文件``config/regions.yml  ``
打开 ``load_regions.rb ``内容如下:

REGIONS = HashWithIndifferentAccess.new(YAML.load(File.read('config/regions.yml')))

想了想,改为以下内容,把``config/regions.yml  ``的相对路径补全

REGIONS = HashWithIndifferentAccess.new(YAML.load(File.read('../config/regions.yml')))

又报错了:

/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:492:in load_missing_constant': Circular dependency detected while autoloading constant AdminUser (RuntimeError) from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:184:inconst_missing' from /home/chenx/iceage-master/app/admin/admin_user.rb:1:in <top (required)>' from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:inload' from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `block in load_file' . . . . . .

麻烦大家帮忙看一下这个什么问题?

这个 spring 只是加速的吧,与那个 java 的没关系。

开发阶段一般默认 development,不用 production。 建议初学从头开始写,不要介入别的复杂项目。

只是同名而已,它不会引起问题。出问题的是你在别的项目里,建议你重新创建一个纯净的 rails 项目,把一些常用的 gem 熟悉一下,然后再玩玩 production 环境,等都熟悉了再折腾旧项目。

一个项目跑起来,如果缺少 yml 文件,我猜是它把 production 环境文件放到 gitignore 了,你可以到它部署环境找找这个文件,或者本地把它的 gem 重新按照文档安装一遍,创建默认的 yml。

  1. Autoload 不正确,请文件和类名保持 Rails 的约定比如 AdminUser -> admin_user.rb
  2. 如果你的 Autoload 不正确 Spring 的 reload 机制可能会有问题,导致卡住,这个时候你需要杀掉 spring 进程
  3. Ruby on Rails 简称是 Rails,不是 ROR!

#3 楼 @huacnlee 谢!但是我仔细过了一下源码对应起来了,麻烦问一下有没有其他的可能出问题的地方呢?

#4 楼 @joke1943 之前报错说AdminUser被循环载入,没有看到未见无法直接分析,不过你这个app/admin下放的是哪部分的代码呢……

#5 楼 @cassiuschen 这个是一个后台管理的模块,我把这个模块删除了,就好了

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