设计没有为密码重置调用正确的控制器

时间:2014-08-07 19:59:21

标签: ruby-on-rails ruby devise routes passwords

在我的Ruby on Rails应用程序上我正在使用Devise for user auth。

我的问题是通过电子邮件恢复密码 - 电子邮件被发送,但生成的链接调用UserController,而不是调用PasswordsController。

以下是我的文件:

模型/ user.rb

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :trackable, :omniauthable,      :omniauth_providers => [:google_oauth2]
...

视图/设计/共享/ _links.haml

...
=link_to "Forgot your password?", new_user_password_path(resource_name)
...

配置/ routes.rb中

# user authentication
devise_for :users, :controllers => { :passwords => "devise/passwords", :omniauth_callbacks => "users/omniauth_callbacks" }

运行佣金路线

...
user_password      POST     /users/password(.:format)                                               devise/passwords#create
new_user_password  GET      /users/password/new(.:format)                                           devise/passwords#new
edit_user_password GET      /users/password/edit(.:format)                                          devise/passwords#edit
                   PATCH    /users/password(.:format)                                               devise/passwords#update
                   PUT      /users/password(.:format)                                               devise/passwords#update
...

电子邮件链接

http://ugolanguage-testing.herokuapp.com/users/password/edit.3?reset_password_token=BHybmnzKnsK3L-xdvRGm

错误消息

2014-08-07T19:36:10.157145+00:00 heroku[router]: at=info method=GET path="/assets/webfonts/ss-standard-e13c0260a956bb5b5817b9fb5dc8de78.woff" host=ugolanguage-testing.herokuapp.com request_id=14f68207-c73a-41b6-84d6-e67709ffc4fc fwd="207.58.203.50" dyno=web.1 connect=1ms service=3ms status=304 bytes=1372
2014-08-07T19:36:20.834814+00:00 app[web.1]: Started GET "/users/password/edit.3?reset_password_token=[FILTERED]" for 207.58.203.50 at 2014-08-07 19:36:20 +0000
2014-08-07T19:36:21.225014+00:00 app[web.1]: Processing by UsersController#edit as 
2014-08-07T19:36:21.255102+00:00 app[web.1]: Completed 500 Internal Server Error in 30ms
2014-08-07T19:36:21.225027+00:00 app[web.1]:   Parameters: {"reset_password_token"=>"[FILTERED]", "id"=>"password"}
2014-08-07T19:36:22.739300+00:00 heroku[router]: at=info method=GET path="/users/password/edit.3?reset_password_token=BHybmnzKnsK3L-xdvRGm" host=ugolanguage-testing.herokuapp.com request_id=d0cd5b9a-3913-4e84-b69c-c7a2508dc019 fwd="207.58.203.50" dyno=web.1 connect=1ms service=1919ms status=500 bytes=1314
2014-08-07T19:36:22.737206+00:00 app[web.1]: 
2014-08-07T19:36:22.737213+00:00 app[web.1]:   app/controllers/users_controller.rb:55:in `check_admin'
2014-08-07T19:36:22.737211+00:00 app[web.1]: NoMethodError (undefined method `admin?' for nil:NilClass):

从错误消息中我得到了一个意义,即Devise期待

<address to password controller><id><token>

出了点问题,因为它假设&lt; id&gt;是地址的“密码”部分,然后它假设控制器由“/ user”给出,它正确地默认调用UserController。

阅读Devise文档,我甚至添加了

devise_scope :user do
  get '/password/edit' => "password#edit"
end

这应该只是现有Devise控制器的自定义操作所必需的,但没有任何改变。

顺便说一下,我确实有一个controller / devise / password_controllers.rb

我知道如何解决这个问题?我可能会从routes.rb中删除“devise_for”,并手动执行所有设计路径。

另外,这就是

视图/设计/邮寄者/ reset_password_instructions.html.haml:

...
%p= link_to 'Change my password', edit_user_password_url(@resource, :reset_password_token => @token)
...

2 个答案:

答案 0 :(得分:1)

你的config / routes.rb中的

# user authentication
devise_for :users, :controllers => { :passwords => "devise/passwords", :omniauth_callbacks => "users/omniauth_callbacks" }
似乎你已经指示设计使用新的控制器来设置密码,你是否创建了控制器,你要求设计使用一个名为密码的控制器,它存在于app / controllers / devise中,而不是gem的控制器。

您是否创建了此控制器,其中的代码是什么。如果没有,你的设计路线应该是:

# user authentication
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

答案 1 :(得分:1)

谢谢你的回答!

是的,我已经从devise的github中复制了password_controller.rb的代码并将其放在app / controllers / devise文件夹中。我也试过没有它,没有提到在route.rb上的devise_for。

我确实发现了错误 - 在routes.rb上,这些行导致rails将我的请求路由到错误的地方:

get "users/new" => "users#new", as: :new_user
post "users" => "users#create"
get "users/:id/edit" => "users#edit", as: :edit_user
patch "users/:id" => "users#update", as: :user

这是由之前的开发人员完成的,他在没有&#34;可恢复的情况下安装了Devise&#34;选项。将切换他的代码,以便我可以回到设计&#34; devise_for&#34;的默认路由行为。

谢谢!

<强>更新

更简单,只需切换订单....现在我的routes.rb文件看起来像这样

# user authentication
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

get "users/new" => "users#new", as: :new_user
post "users" => "users#create"
get "users/:id/edit" => "users#edit", as: :edit_user
patch "users/:id" => "users#update", as: :user
delete "/users/:id" =>   "users#destroy"