设计:密码重置电子邮件中的edit_password_url将用户发送到url / api / v1 /

我有我的rails应用程序和Devise设置使用JSON API来进行用户注册和登录。 副作用是密码重置电子邮件中的edit_password_url意外地将用户发送到:

  http://localhost:3000/api/v1/password/edit?reset_password_token=ZzyPCgmspN2964ENUkSS 

什么时候它不应该有api/v1/ ,并且应该发送给:

 http://localhost:3000/password/edit?reset_password_token=ZzyPCgmspN2964ENUkSS 

我一直在寻找,但无法弄清楚在哪里解决这个问题。

我创建了以下内容:

 Api::V1::SessionsController < Devise::SessionsController 

 Api::V1::RegistrationsController < RegistrationsController 

我有一个常规的RegistrationsControllerinheritance自devise,但不是一个常规的SessionsController,所以我只是直接从设计inheritance。

谢谢您的帮助!

编辑:

routes.rb

 namespace :api, defaults: {format: 'json'} do namespace :v1 do resources :users devise_for :users, :path => '', path_names: {sign_in: "login", sign_out: "logout"}, controllers: { omniauth_callbacks: "authentications", registrations: "registrations"} end end devise_for :users, :path => '', path_names: {sign_in: "login", sign_out: "logout"}, controllers: { omniauth_callbacks: "authentications", registrations: "registrations"} resources :users 

编辑2: rake routes输出

  new_api_v1_user_session GET /api/v1/login(.:format) api/v1/sessions#new {:format=>"json"} api_v1_user_session POST /api/v1/login(.:format) api/v1/sessions#create {:format=>"json"} destroy_api_v1_user_session DELETE /api/v1/logout(.:format) api/v1/sessions#destroy {:format=>"json"} api_v1_user_omniauth_authorize GET|POST /auth/:provider(.:format) authentications#passthru {:provider=>/twitter|facebook/, :format=>"json"} api_v1_user_omniauth_callback GET|POST /auth/:action/callback(.:format) authentications#(?-mix:twitter|facebook) {:format=>"json"} api_v1_user_password POST /api/v1/password(.:format) api/v1/passwords#create {:format=>"json"} new_api_v1_user_password GET /api/v1/password/new(.:format) api/v1/passwords#new {:format=>"json"} edit_api_v1_user_password GET /api/v1/password/edit(.:format) api/v1/passwords#edit {:format=>"json"} PUT /api/v1/password(.:format) api/v1/passwords#update {:format=>"json"} cancel_api_v1_user_registration GET /api/v1/cancel(.:format) registrations#cancel {:format=>"json"} api_v1_user_registration POST /api/v1(.:format) registrations#create {:format=>"json"} new_api_v1_user_registration GET /api/v1/sign_up(.:format) registrations#new {:format=>"json"} edit_api_v1_user_registration GET /api/v1/edit(.:format) registrations#edit {:format=>"json"} PUT /api/v1(.:format) registrations#update {:format=>"json"} DELETE /api/v1(.:format) registrations#destroy {:format=>"json"} sessions GET /sessions(.:format) sessions#index POST /sessions(.:format) sessions#create new_session GET /sessions/new(.:format) sessions#new edit_session GET /sessions/:id/edit(.:format) sessions#edit session GET /sessions/:id(.:format) sessions#show PUT /sessions/:id(.:format) sessions#update DELETE /sessions/:id(.:format) sessions#destroy authentications GET /authentications(.:format) authentications#index POST /authentications(.:format) authentications#create new_authentication GET /authentications/new(.:format) authentications#new edit_authentication GET /authentications/:id/edit(.:format) authentications#edit authentication GET /authentications/:id(.:format) authentications#show PUT /authentications/:id(.:format) authentications#update DELETE /authentications/:id(.:format) authentications#destroy new_user_session GET /login(.:format) devise/sessions#new user_session POST /login(.:format) devise/sessions#create destroy_user_session DELETE /logout(.:format) devise/sessions#destroy user_omniauth_authorize GET|POST /auth/:provider(.:format) authentications#passthru {:provider=>/twitter|facebook/} user_omniauth_callback GET|POST /auth/:action/callback(.:format) authentications#(?-mix:twitter|facebook) user_password POST /password(.:format) devise/passwords#create new_user_password GET /password/new(.:format) devise/passwords#new edit_user_password GET /password/edit(.:format) devise/passwords#edit PUT /password(.:format) devise/passwords#update cancel_user_registration GET /cancel(.:format) registrations#cancel user_registration POST / registrations#create new_user_registration GET /sign_up(.:format) registrations#new edit_user_registration GET /edit(.:format) registrations#edit PUT / registrations#update DELETE / registrations#destroy 

编辑3:

所以我一直在测试一些东西,在设计电子邮件模板中,路径edit_password_url就在那里,并且用于生成上面错误的url,但是当我做rake routes ,只存在edit_user_password_url

看看Devise Controller URL Helpers doc(在这里找到),我会用到:

edit_password_path(:user) ,转换为edit_user_password_pathpath似乎可以与url互换。

我不是100%肯定,但这一行定义了一个名为edit_password_path的方法,而这一行在 Devise上下文中创建一个路由……

你没有发布你的routes.rb但我猜你想要/password/edit路由到没有api/v1/ URL的’Api / V1 / RegistrationsController’?

如果是,那么您需要使用路由DSL的module选项。 像这样:

 scope module: 'api/v1/' do resources :sessions, :registrations end 

当然你需要在devise_for调用中集成上面的devise_for 。 我不是设计专家,我猜,你需要使用devise_scope而不是像这样的scope

 devise_scope module: 'api/v1/' do resources :sessions, :registrations end 

注意:如果上述方法无效。 用您的routes.rbroutes.rb 。 我们会帮您修复它

根据生成的路线,你应该试试这个。 在我的情况下,它的工作正常。 试试这个:

 edit_user_password_url(reset_password_token: @token) 

所以,奇怪的是,我需要更改设计邮件模板中的路径。 我从edit_password_url更改了它,它生成了一个url,但没有显示在我的rake routes输出中, edit_user_password_url在我的rake routes输出中找到的edit_user_password_url

我很想知道为什么edit_password_url工作,即使它没有显示在rake routes输出中,并且非常乐意给那些可以解释发生在我身上的人分配正确的答案。