路由错误 - Ruby on Rails

时间:2011-07-06 23:27:51

标签: ruby-on-rails ruby-on-rails-3

我的路线文件中已经有了这个:

namespace :api do
    root :to => 'graphs#index' #default page when accessing /admin
    resources :graphs, :defaults => { :format => 'json' }

    match ':graphs/:id(/:method)'
  end

但是当我试图打开时:

mydomain.com/api 

mydomain.com/api/graphs/

我收到以下错误:

2011-07-06T23:12:06+00:00 app[web.1]: /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/mapper.rb:171:in `default_controller_and_action': missing :action (ArgumentError)
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/mapper.rb:72:in `normalize_options!'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/mapper.rb:55:in `initialize'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/mapper.rb:272:in `new'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/mapper.rb:272:in `match'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/mapper.rb:1173:in `match'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/mapper.rb:1360:in `match'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/config/routes.rb:84:in `block (2 levels) in <top (required)>'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/mapper.rb:624:in `block in namespace'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/mapper.rb:546:in `scope'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/mapper.rb:624:in `namespace'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/mapper.rb:1119:in `namespace'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/config/routes.rb:80:in `block in <top (required)>'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/route_set.rb:233:in `instance_exec'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.9/lib/action_dispatch/routing/route_set.rb:233:in `draw'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/config/routes.rb:1:in `<top (required)>'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in `load'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in `block in load'
2011-07-06T23:12:06+00:00 app[web.1]:   from /app/.bundle/gems/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:227:in `load_dependency'
2011-07-06T23:12:06+00:00 app[web.1]:   from <internal:lib/rubygems/custom_require>:29:in `require'
2011-07-06T23:12:06+00:00 heroku[web.1]: Process exited
2011-07-06T23:12:07+00:00 heroku[web.1]: State changed from starting to crashed

2 个答案:

答案 0 :(得分:1)

不可否认,我对匹配声明并不熟悉,但看起来你错过了它的第二部分,例如:

match ':graphs/:id(/:method)' => 'pages#something'

正如我在这里看到的那样:

Rails routes match full hostname with multiple period in between

在这里:

understanding rails routes: match vs root in routes.rb

这是一个similar问题,其中“to”中的操作没有正确说明。

答案 1 :(得分:0)

我在RESTful路线中也有同样的问题。

以下是示例:

    resources :categories do
      member do
        post 'sort/:move', constraints: { move: /up|down/ }
      end
    end

请求“/ categories / 1 / sort / up”会导致同样的错误。

你可以看到存在:移动参数误导控制器/动作使用的轨道。 因此,您需要明确指定“操作”选项:

    post 'sort/:move', constraints: { move: /up|down/ }, action: 'sort'

注意如果使用“to”选项,则不能省略控制器名称:

    post 'sort/:move', constraints: { move: /up|down/ }, to: 'categories#sort'