新手又来提问了:关于rails4.0.0使用grape做api(ruby2.0.0)

2013-08-25 01:06:32 +08:00
 androidBrant
新建的api文件 app/api/api.rb

http://gist.github.com/c6eb81aeebe4fa347b3b

在routes.rb中添加
mount JokesApi::API => "/"

修改application.rb

http://gist.github.com/6329221

添加gem gem ‘grape’

rails s 启动服务器报错,信息如下:

=> Booting WEBrick
=> Rails 4.0.0.rc1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/sjwu/Desktop/joke_app/config/routes.rb:5:in `block in <top (required)>': uninitialized constant JokesApi (NameError)
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0.rc1/lib/action_dispatch/routing/route_set.rb:315:in `instance_exec'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0.rc1/lib/action_dispatch/routing/route_set.rb:315:in `eval_block'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0.rc1/lib/action_dispatch/routing/route_set.rb:293:in `draw'
from /Users/sjwu/Desktop/joke_app/config/routes.rb:2:in `<top (required)>'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/application/routes_reloader.rb:40:in `each'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/application/routes_reloader.rb:40:in `load_paths'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/application/routes_reloader.rb:16:in `reload!'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/application/routes_reloader.rb:26:in `block in updater'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0.rc1/lib/active_support/file_update_checker.rb:75:in `call'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0.rc1/lib/active_support/file_update_checker.rb:75:in `execute'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/application/routes_reloader.rb:27:in `updater'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/application/routes_reloader.rb:6:in `execute_if_updated'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/initializable.rb:30:in `run'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/tsort.rb:180:in `each'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/application.rb:214:in `initialize!'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/sjwu/Desktop/joke_app/config/environment.rb:5:in `<top (required)>'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/backports-3.3.3/lib/backports/tools.rb:328:in `require'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/backports-3.3.3/lib/backports/tools.rb:328:in `require_with_backports'
from /Users/sjwu/Desktop/joke_app/config.ru:3:in `block in <main>'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
from /Users/sjwu/Desktop/joke_app/config.ru:in `new'
from /Users/sjwu/Desktop/joke_app/config.ru:in `<main>'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/commands/server.rb:48:in `app'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/commands/server.rb:75:in `start'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/commands.rb:80:in `block in <top (required)>'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/commands.rb:75:in `tap'
from /Users/sjwu/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/railties-4.0.0.rc1/lib/rails/commands.rb:75:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'



高手帮忙看下,是啥原因,新学rails不久,搞不明白,为什么JokeApi文件没有自己加载?

谢谢。。。
4731 次点击
所在节点    程序员
12 条回复
androidBrant
2013-08-25 01:10:42 +08:00
第一个app: http://192.157.213.221 想作api来作移动端。搞几天了这错误搞不定。。
coolzilj
2013-08-25 23:53:30 +08:00
@androidBrant 修改`application.rb`文件了么?
lichao
2013-08-25 23:56:59 +08:00
怎么还是 RC1
coolzilj
2013-08-26 00:13:27 +08:00
@androidBrant 会不会是文件路径的原因,试下改成 app/api/jokes_api/api.rb
reducm
2013-08-26 07:15:16 +08:00
routes文件先require一下Api文件
androidBrant
2013-08-26 09:05:00 +08:00
@coolzilj 修改了
@coolzilj 路径会有影响吗?config.paths.add "app/api", glob: "**/*.rb"
config.autoload_paths += Dir["#{Rails.root}/app/api/*"] 这两句是不是对指定的api路径自动require?
@reducm config.paths.add "app/api", glob: "**/*.rb"
config.autoload_paths += Dir["#{Rails.root}/app/api/*"]这两句就是对指定的api路径自动require。。。
androidBrant
2013-08-26 09:06:12 +08:00
@lichao 哪个rc1?有碰到过类似的问题吗?还是有可能的解决办法?
lichao
2013-08-26 09:09:31 +08:00
@androidBrant 我意思是 Rails 4 早已正式 Release 了,你怎么还用 Release Candidate 1
coolzilj
2013-08-26 09:46:07 +08:00
@androidBrant 是自动require没错,但你没遵守rails里的文件命名惯例
coolzilj
2013-08-26 10:15:00 +08:00
@androidBrant

任何声明在 autoload paths 的 module 只有在 module 名字和父目录名相同时才会载入。
举个例子:

```ruby

# lib/my_stuff/bar.rb

module Foo
class Bar
end
end
```
注意文件名,这样子是不会正确自动载入的,但如果你改成 lib/foo/bar.rb 它就可以了。

关于autoload背后魔法的详细解释,可以参考这里

http://stackoverflow.com/questions/3356742/best-way-to-load-module-class-from-lib-folder-in-rails-3?answertab=active#tab-top
androidBrant
2013-08-26 10:47:49 +08:00
@coolzilj 哦,规则还是强制性的啊,我看看,Thank you
hlxwell
2014-08-14 18:59:26 +08:00
Rails 4 里 app 下的目录都会被加载,但是在 dev 环境下是默认 lazy loaded, 所以你需要手动 require 一下,或者在 routes.rb 顶上加上.

`Rails.application.eager_load! if Rails.env.development?`

就搞定了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/80123

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX