我正在看書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>
運行'bundle exec rake routes'並查看可用的路由。 – 2014-09-27 18:13:08
Зелёный,我添加了輸出。 – 2014-09-27 18:15:40