如何在使用Devise首次登录后重置密码

时间:2017-03-21 16:09:24

标签: ruby-on-rails ruby devise

我尝试在用户首次使用“How To: Allow users to edit their password”作为参考登录后实施密码重置。

我收到以下错误:

Routing Error

No route matches [GET] "/user/update_password"

我在帖子的末尾添加了完整的跟踪。

我错过了什么?

我正在使用Rails的4.2.6'。

应用程序控制器:

class ApplicationController < ActionController::Base
  after_filter :reset_last_captcha_code!
  protect_from_forgery with: :exception


  def after_sign_in_path_for(resource)
    if current_user.sign_in_count == 1
      update_password_user_path
    else
      root_path
    end
  end
end

查看:

<%= form_for(@user, :url => { :action => "update_password" } ) do |f| %>
  <div class="field">
    <%= f.label :password, "Password" %><br />
    <%= f.password_field :password, :autocomplete => "off"  %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="action_container">
    <%= f.submit %>
  </div>
<% end %>

路线:

  resource :user, only: [:edit] do
    collection do
      patch 'update_password'
    end
  end

用户模型:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

完整追踪:

actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.5) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.5) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.5) lib/rack/lock.rb:17:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.5) lib/rack/sendfile.rb:113:in `call'
railties (4.2.6) lib/rails/engine.rb:518:in `call'
railties (4.2.6) lib/rails/application.rb:165:in `call'
rack (1.6.5) lib/rack/lock.rb:17:in `call'
rack (1.6.5) lib/rack/content_length.rb:15:in `call'
rack (1.6.5) lib/rack/handler/webrick.rb:88:in `service'
/Users/jj/.rbenv/versions/2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
/Users/jj/.rbenv/versions/2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
/Users/jj/.rbenv/versions/2.3.0/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'

3 个答案:

答案 0 :(得分:0)

您的路线是PATCH,但您的表单正在点击GET端点。尝试将视图中的第一行更改为:

<%= form_for(@user, :url => { :action => "update_password" }, :method => patch ) do |f| %>

答案 1 :(得分:0)

如果您正在使用设计,除非您有充分理由不这样做,否则通常会更容易使用他们已经实施的内容。您在config / routes.rb文件中只需要:

devise_for :users

在您完成此操作后,从您的终端运行rake路线,您应该会看到更改注册,密码,删除帐户等的路线列表。如果您真的想要懒惰,可以从终端:

rails g devise:install views

这将为您提供管理用户所需的所有表单和视图。然后将人们链接到您只需要使用已为您设置的路线的视图。运行

rake routes

您将看到需要链接的路线。例如a要创建一个登录按钮,link_to new_user_session_path。对于注册按钮,它将是link_to new_user_registration_path。导致的页面具有重置密码/删除帐户等的简单链接...

答案 2 :(得分:0)

您可以像devise推荐的那样使用这些路线。在routes.rb而不是:

  resource :user, only: [:edit] do
    collection do
      patch 'update_password'
    end
  end

使用:

devise_for :users

有关如何将其转换为Rails路由check out this documentation

的更多信息