2014-09-27 171 views
0

我正在看書Rails 4路和玩路由。以下是我的環境信息:Rails路由問題,沒有路由匹配[GET]「/ api/v1/test_api」

C:\rails_projects\blog>rake about 
About your application's environment 
Ruby version    1.9.3-p374 (i386-mingw32) 
RubyGems version   1.8.29 
Rack version    1.5 
Rails version    4.1.6 
JavaScript Runtime  Node.js (V8) 
Active Record version  4.1.6 
Action Pack version  4.1.6 
Action View version  4.1.6 
Action Mailer version  4.1.6 
Active Support version 4.1.6 
Middleware    Rack::Sendfile, ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x40279b0>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag 
Application root   C:/rails_projects/blog 
Environment    development 
Database adapter   sqlite3 
Database schema version 20140925163909 

C:\rails_projects\blog> 

我的routes.rb文件是:

Rails.application.routes.draw do 
    resources :posts 
    root "posts#index" 

    match "api/v1/:api" => redirect(status: 302) {|params| "/api/" + params[:api] + "_v2"}, via: :any 
end 

我的問題是,在routes.rb中的match線。據我瞭解,如果我訪問http://localhost:3000/api/v1/test_api,路由規則應該帶我到http://localhost:3000/api/test_api_v2。但是當我訪問http://localhost:3000/api/v1/test_api時,我得到了以下錯誤:

No route matches [GET] "/api/v1/test_api" 

Rails.root: C:/rails_projects/blog 

我的問題在哪裏?我對路由規則的理解有什麼問題嗎?


編輯,從rake routes

C:\rails_projects\blog>rake routes 
    Prefix Verb URI Pattern    Controller#Action 
    posts GET /posts(.:format)   posts#index 
      POST /posts(.:format)   posts#create 
new_post GET /posts/new(.:format)  posts#new 
edit_post GET /posts/:id/edit(.:format) posts#edit 
    post GET /posts/:id(.:format)  posts#show 
      PATCH /posts/:id(.:format)  posts#update 
      PUT /posts/:id(.:format)  posts#update 
      DELETE /posts/:id(.:format)  posts#destroy 
    root GET /      posts#index 
      ANY /api/v1/:api(.:format) redirect(302) 

C:\rails_projects\blog> 
+0

運行'bundle exec rake routes'並查看可用的路由。 – 2014-09-27 18:13:08

+0

Зелёный,我添加了輸出。 – 2014-09-27 18:15:40

回答

1

增加輸出我覺得你只需要一個/在你的比賽開始:match "/api/v1/:api"...。你確定:any是一個有效的選擇? Rails Guide只提到:all

+0

謝謝你dchop1!是的,問題是「:any」部分而不是「/」字符。當我改成':all'時,它就會起作用。我使用':any',因爲它是我提到的書中的一個例子。再次感謝! – 2014-09-28 09:19:11

相關問題