【问题标题】:Devise routes for API为 API 设计路线
【发布时间】:2015-03-10 09:59:25
【问题描述】:

我如何理解以下错误消息(在测试时)以及为什么在向我的注册控制器创建操作发出发布请求时,我使用的是/lnf.json

在我的路线中,我有以下约束和路径设置

constraints(subdomain: 'api') do
  devise_for :users, path: 'lnf', controllers: { registrations: "registrations" }
end

还有我的注册控制器

class RegistrationsController < Devise::RegistrationsController
  skip_before_action :verify_authenticity_token
  respond_to :json

  def create
   super
  end

end

如果我通过 curl 向http://api.local.dev:3000/lnf.json 发出帖子请求,我会得到所需的响应。

当我想使用 Rspec 进行测试时,我有一个简单的测试设置

post :create, format: :json, :user => {user params here}

但我在运行测试时遇到错误

AbstractController::ActionNotFound:
Could not find devise mapping for path "/lnf.json?

编辑

按要求输出 Rake 路由

 new_user_session GET    /lnf/sign_in(.:format)          devise/sessions#new {:subdomain=>"api"}
        user_session POST   /lnf/sign_in(.:format)          devise/sessions#create {:subdomain=>"api"}
destroy_user_session DELETE /lnf/sign_out(.:format)         devise/sessions#destroy {:subdomain=>"api"}
       user_password POST   /lnf/password(.:format)         devise/passwords#create {:subdomain=>"api"}
   new_user_password GET    /lnf/password/new(.:format)     devise/passwords#new {:subdomain=>"api"}
  edit_user_password GET    /lnf/password/edit(.:format)    devise/passwords#edit {:subdomain=>"api"}
                     PATCH  /lnf/password(.:format)         devise/passwords#update {:subdomain=>"api"}
                     PUT    /lnf/password(.:format)         devise/passwords#update {:subdomain=>"api"}
   user_confirmation POST   /lnf/confirmation(.:format)     devise/confirmations#create {:subdomain=>"api"}
 new_user_confirmation GET    /lnf/confirmation/new(.:format) devise/confirmations#new {:subdomain=>"api"}

注册控制器

 cancel_user_registration GET    /lnf/cancel(.:format)           registrations#cancel {:subdomain=>"api"}
   user_registration POST   /lnf(.:format)                  registrations#create {:subdomain=>"api"}
  new_user_registration GET    /lnf/sign_up(.:format)          registrations#new {:subdomain=>"api"}
 edit_user_registration GET    /lnf/edit(.:format)             registrations#edit {:subdomain=>"api"}
                     PATCH  /lnf(.:format)                  registrations#update {:subdomain=>"api"}
                     PUT    /lnf(.:format)                  registrations#update {:subdomain=>"api"}
                     DELETE /lnf(.:format)                  registrations#destroy {:subdomain=>"api"}
                 lnf POST   /lnf(.:format)                  registrations#create {:subdomain=>"api", :format=>"json"}

【问题讨论】:

    标签: ruby-on-rails ruby json rspec devise


    【解决方案1】:

    看起来 devise_for 路由部分需要稍微调整一下(或者为您的情况添加特殊路由 lnf aka "post '/lnf', to: 'registrations#create', defaults: {format: 'json'}" )。 你能把'rake routes | 的输出grep devise' 命令在这里?

    【讨论】:

    • 谢谢,我已经更新了注册路线#create,你想要所有的路线吗?
    • 有了这条特殊路线,我将如何覆盖当前设置提供的路线?
    • 你能告诉我rake routes | grep devise的输出
    • 您是否尝试在测试参数中使用 :subdomain=>"api"? post :create, subdomain: "api", format: :json, :user => {user params here} 我想你可以从这个讨论中得到一些想法github.com/rspec/rspec-rails/issues/789
    • 是的,我试过了,subdomain: :apisubdomain: 'api'
    猜你喜欢
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    相关资源
    最近更新 更多