我正在看书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