2011-07-06 119 views
0

我已經有這個在我的路線文件:路線錯誤 - Ruby on Rails的

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 
+0

你運行'耙routes'查看路線圖? –

回答

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問題在「到」中的操作未正確說明。

+0

你建議我使用別的東西而不是匹配嗎?如果我想做這樣的事情:match':graphs /:id(/:method)'=>'pages#:methodFromUrl' – glarkou

+0

我認爲這裏有一個例子 - http://edgeguides.rubyonrails.org/ routing.html - 例如match':controller /:action /:id/with_user /:user_id' 此路線將響應諸如/ photos/show/1/with_user/2之類的路徑。在這種情況下,params會是{:controller =>「photos」,:action =>「show」,:id =>「1」,:user_id =>「2」}。 –

0

我在RESTful路由中也遇到同樣的問題。

這裏是例如:

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

請求 「/類別/ 1 /排序/向上」 導致相同的錯誤。

正如你所看到的那樣:移動參數會誤導控制器/動作使用的軌道。 所以你需要指定「動作」選項明確:

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

注意,如果使用「到」選項不能省略控制器名稱:

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