V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
androidBrant
V2EX  ›  程序员

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

  •  
  •   androidBrant · 2013-08-25 01:06:32 +08:00 · 4731 次点击
    这是一个创建于 4134 天前的主题,其中的信息可能已经有所发展或是发生改变。
    新建的api文件 app/api/api.rb

    http://gist.github.com/c6eb81aeebe4fa347b3b

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

    修改application.rb



    添加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文件没有自己加载?

    谢谢。。。
    12 条回复    2014-08-14 18:59:26 +08:00
    androidBrant
        1
    androidBrant  
    OP
       2013-08-25 01:10:42 +08:00
    第一个app: http://192.157.213.221 想作api来作移动端。搞几天了这错误搞不定。。
    coolzilj
        2
    coolzilj  
       2013-08-25 23:53:30 +08:00
    @androidBrant 修改`application.rb`文件了么?
    lichao
        3
    lichao  
       2013-08-25 23:56:59 +08:00 via iPhone
    怎么还是 RC1
    coolzilj
        4
    coolzilj  
       2013-08-26 00:13:27 +08:00
    @androidBrant 会不会是文件路径的原因,试下改成 app/api/jokes_api/api.rb
    reducm
        5
    reducm  
       2013-08-26 07:15:16 +08:00 via Android
    routes文件先require一下Api文件
    androidBrant
        6
    androidBrant  
    OP
       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
        7
    androidBrant  
    OP
       2013-08-26 09:06:12 +08:00
    @lichao 哪个rc1?有碰到过类似的问题吗?还是有可能的解决办法?
    lichao
        8
    lichao  
       2013-08-26 09:09:31 +08:00
    @androidBrant 我意思是 Rails 4 早已正式 Release 了,你怎么还用 Release Candidate 1
    coolzilj
        9
    coolzilj  
       2013-08-26 09:46:07 +08:00
    @androidBrant 是自动require没错,但你没遵守rails里的文件命名惯例
    coolzilj
        10
    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
        11
    androidBrant  
    OP
       2013-08-26 10:47:49 +08:00
    @coolzilj 哦,规则还是强制性的啊,我看看,Thank you
    hlxwell
        12
    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?`

    就搞定了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1004 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 20:16 · PVG 04:16 · LAX 12:16 · JFK 15:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.