无法找到路线

时间:2014-03-17 14:19:18

标签: ruby-on-rails rspec ruby-on-rails-4 routes

我正在尝试通过我的应用程序中的api取消订阅用户。我已经使用api / subscriptions_controller文件公开了这个api。

我的路线档案是

  namespace :api, defaults: {format: 'json'} do
    match '/unsubscribe', to: 'subscriptions#unsubscribe', via: [:post]
  end

此api接受电子邮件地址并取消订阅用户。

Rake路线给出了正确的网址,所以我知道它就在那里。

api_unsubscribe POST /api/unsubscribe(.:format)api / subscriptions #process {:format =>“json”}

当我在rspec中运行我的测试时,我在这一行上收到错误。

post "/unsubscribe", @my_params, @authentication_params

没有路线匹配[POST]“/ unsubscribe”

我尝试了很多不同的变体,但似乎并不想工作。

有什么想法吗?

感谢。

服务器日志

Started GET "/unsubscribe" for 127.0.0.1 at 2014-03-17 16:31:01 +0200

ActionController::RoutingError (No route matches [GET] "/unsubscribe"):
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/railties/lib/rails/rack/logger.rb:38:in `call_app'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/railties/lib/rails/rack/logger.rb:20:in `block in call'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/activesupport/lib/active_support/tagged_logging.rb:68:in `block in tagged'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/activesupport/lib/active_support/tagged_logging.rb:26:in `tagged'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/activesupport/lib/active_support/tagged_logging.rb:68:in `tagged'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/railties/lib/rails/rack/logger.rb:20:in `call'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/actionpack/lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/actionpack/lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/railties/lib/rails/engine.rb:515:in `call'
  /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/railties/lib/rails/application.rb:142:in `call'
  rack (1.5.2) lib/rack/urlmap.rb:65:in `block in call'
  rack (1.5.2) lib/rack/urlmap.rb:50:in `each'
  rack (1.5.2) lib/rack/urlmap.rb:50:in `call'
  passenger (4.0.38) lib/phusion_passenger/rack/thread_handler_extension.rb:77:in `process_request'
  passenger (4.0.38) lib/phusion_passenger/request_handler/thread_handler.rb:142:in `accept_and_process_next_request'
  passenger (4.0.38) lib/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
  passenger (4.0.38) lib/phusion_passenger/request_handler.rb:448:in `block (3 levels) in start_threads'


  Rendered /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.2ms)
  Rendered /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/actionpack/lib/action_dispatch/middleware/templates/routes/_route.html.erb (9.5ms)
  Rendered /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/actionpack/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.3ms)
  Rendered /Users/ryanme/.rvm/gems/ruby-2.1.0@crm/bundler/gems/rails-daf7e2553fe9/actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (54.4ms)

我的整个佣金路线。请参阅底部的api相关内容。

new_user_session_path    GET     /users/sign_in(.:format)    devise/sessions#new
user_session_path    POST    /users/sign_in(.:format)    devise/sessions#create
destroy_user_session_path    DELETE  /users/sign_out(.:format)   devise/sessions#destroy
user_password_path   POST    /users/password(.:format)   devise/passwords#create
new_user_password_path   GET     /users/password/new(.:format)   devise/passwords#new
edit_user_password_path  GET     /users/password/edit(.:format)  devise/passwords#edit
PATCH    /users/password(.:format)   devise/passwords#update
PUT  /users/password(.:format)   devise/passwords#update
cancel_user_registration_path    GET     /users/cancel(.:format)     devise/registrations#cancel
user_registration_path   POST    /users(.:format)    devise/registrations#create
new_user_registration_path   GET     /users/sign_up(.:format)    devise/registrations#new
edit_user_registration_path  GET     /users/edit(.:format)   devise/registrations#edit
PATCH    /users(.:format)    devise/registrations#update
PUT  /users(.:format)    devise/registrations#update
DELETE   /users(.:format)    devise/registrations#destroy
authenticated_root_path  GET     /   dashboard#index
unauthenticated_root_path    GET     /   devise/sessions#new
interactions_path    GET     /interactions(.:format)     interactions#index
POST     /interactions(.:format)     interactions#create
new_interaction_path     GET     /interactions/new(.:format)     interactions#new
edit_interaction_path    GET     /interactions/:id/edit(.:format)    interactions#edit
interaction_path     GET     /interactions/:id(.:format)     interactions#show
PATCH    /interactions/:id(.:format)     interactions#update
PUT  /interactions/:id(.:format)     interactions#update
DELETE   /interactions/:id(.:format)     interactions#destroy
course_course_presentations_path     GET     /courses/:course_id/course_presentations(.:format)  course_presentations#index
POST     /courses/:course_id/course_presentations(.:format)  course_presentations#create
new_course_course_presentation_path  GET     /courses/:course_id/course_presentations/new(.:format)  course_presentations#new
edit_course_course_presentation_path     GET     /courses/:course_id/course_presentations/:id/edit(.:format)     course_presentations#edit
course_course_presentation_path  GET     /courses/:course_id/course_presentations/:id(.:format)  course_presentations#show
PATCH    /courses/:course_id/course_presentations/:id(.:format)  course_presentations#update
PUT  /courses/:course_id/course_presentations/:id(.:format)  course_presentations#update
DELETE   /courses/:course_id/course_presentations/:id(.:format)  course_presentations#destroy
courses_path     GET     /courses(.:format)  courses#index
POST     /courses(.:format)  courses#create
new_course_path  GET     /courses/new(.:format)  courses#new
edit_course_path     GET     /courses/:id/edit(.:format)     courses#edit
course_path  GET     /courses/:id(.:format)  courses#show
PATCH    /courses/:id(.:format)  courses#update
PUT  /courses/:id(.:format)  courses#update
DELETE   /courses/:id(.:format)  courses#destroy
contacts_path    GET     /contacts(.:format)     contacts#index
POST     /contacts(.:format)     contacts#create
new_contact_path     GET     /contacts/new(.:format)     contacts#new
edit_contact_path    GET     /contacts/:id/edit(.:format)    contacts#edit
contact_path     GET     /contacts/:id(.:format)     contacts#show
PATCH    /contacts/:id(.:format)     contacts#update
PUT  /contacts/:id(.:format)     contacts#update
DELETE   /contacts/:id(.:format)     contacts#destroy
subscription_types_path  GET     /subscription_types(.:format)   subscription_types#index
subscription_type_path   GET     /subscription_types/:id(.:format)   subscription_types#show
interaction_types_path   GET     /interaction_types(.:format)    interaction_types#index
interaction_type_path    GET     /interaction_types/:id(.:format)    interaction_types#show
outcome_types_path   GET     /outcome_types(.:format)    outcome_types#index
outcome_type_path    GET     /outcome_types/:id(.:format)    outcome_types#show
api_interactions_path    POST    /api/interactions(.:format)     api/interactions#create {:format=>"json"}
api_unsubscribe_path     POST    /api/unsubscribe(.:format)  api/subscriptions#unsubscribe {:format=>"json"}

1 个答案:

答案 0 :(得分:1)

根据您的路线,您有

api_unsubscribe POST /api/unsubscribe(.:format) api/subscriptions#process {:format=>"json"}

请注意,您的路线定义为/api/unsubscribe而非/unsubscribe。 因此,错误。

更改

post "/unsubscribe", @my_params, @authentication_params

post "/api/unsubscribe", @my_params, @authentication_params