【问题标题】:Not able to find route找不到路线
【发布时间】:2014-04-22 19:12:30
【问题描述】:

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

我的路线文件是

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

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

Rake 路线为我提供了正确的 url,所以我知道它在那里。

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)

我的整个 rake 路线。请参阅底部的 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"}

【问题讨论】:

  • 不应该是post "/api/unsubscribe", @my_params, @authentication_params之类的吗?
  • 是的,我也这么想并尝试过,但那不起作用。 /api/unsubscribe 也没有
  • 为什么您的 routes.rb 中的 'subscriptions#unsubscribe'rake routes 中的 api/subscriptions#process 匹配? process 动作出现在哪里?
  • 它嵌套在命名空间中:api - 如果我将控制器#action 更改为 'api/subscriptions#unsubscribe' 然后 rake 路由会给出 api_unsubscribe_path POST /api/unsubscribe(.:format) api/ api/subscriptions#process {:format=>"json"} 我认为这是错误的。 api 前缀不应该有两次。
  • Magnuss 正在谈论 #unsubscribe 以及为什么您会收到 #process

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


【解决方案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

【讨论】:

  • 感谢您的帮助,但这给出了同样的错误。我已经尝试过这些东西。我什至尝试将其从路由文件的命名空间中删除,但这也不起作用。 ActionController::RoutingError: 没有路由匹配 [POST] "/api/unsubscribe"
  • 注意,我还在同一个命名空间中公开了一个交互控制器,没有任何问题。这引用了“资源:交互,仅:[:创建]”。只是想我会提到它。
  • 你能分享服务器日志的错误。另外,@my_params@authentication_params 是干什么用的?
  • 我已经添加了日志。 Authentication_params 只是我为身份验证传递的一个令牌(它与我的其他 API 完美配合)。 @myparams 是我使用的发布参数列表。喜欢 email_address 以查找用户并取消订阅。
  • 您是否错误地为/api/unsubscribe 定义了两条路由?请分享您的完整rake routes
猜你喜欢
  • 2016-04-04
  • 2016-09-16
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
  • 1970-01-01
相关资源
最近更新 更多