开发者

Routes Error - Ruby on Rails

开发者 https://www.devze.com 2023-03-17 16:05 出处:网络
I already have this in my routes file: namespace :api do root :to => \'graphs#index\' #default page when accessing /admin

I already have this in my routes file:

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

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

But when I try to open:

mydomain.com/api 

or

mydomain.com/api/graphs/

I got the following error:

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:开发者_JAVA技巧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


Admittedly I'm not that familiar with the match statement, but it looks like you are missing the second part of it, like:

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

As I see here:

Rails routes match full hostname with multiple period in between

and here:

understanding rails routes: match vs root in routes.rb

Here's a similar issue where the action in the "to" was not properly stated.


I also had same issue within RESTful route.

Here is example:

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

Requesting "/categories/1/sort/up" causes same error.

As you can see presence of :move parameter misleads rails which controller/action use. So you need to specify "action" option explicitly:

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

Note if use "to" option you cannot omit controller name:

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

精彩评论

暂无评论...
验证码 换一张
取 消