【问题标题】:Adding password reset functionality to Rails app after Hartl tutorial在 Hartl 教程之后向 Rails 应用程序添加密码重置功能
【发布时间】:2013-03-06 02:15:48
【问题描述】:

我是 RoR 的新手,一直在学习Hartl 教程(非常棒)。我已经成功地完成了第 9 章(稍微调整一下,因为我的最终目标不是制作微博网站)。那时,我决定要在我的应用程序中添加一个“记住我”复选框并重置密码功能,所以我跳到了railscast tutorial(按照 Hartl 的建议)。复选框非常顺利,但我在密码重置部分遇到了障碍。这是一个接一个的错误。我不得不承认我忍不住做了一些调整——我尝试使用form_for 语法而不是form_tag 语法。我已经能够提交电子邮件地址,但随后我收到了No route matches [POST] "/reset_password/new" 消息。在过去的两天里,我一直在阅读有关 stackoverflow 的类似帖子并尝试了这些建议,但我似乎无法想出一些可行的方法。请帮忙!

这里是细节:

我的密码重置视图位于/app/views/reset_password/new.html.erb

<% provide(:title, 'Reset Password') %>
<h1>Reset Password</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for @user, url: new_reset_password_path do |f| %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.submit "Reset Password", class: "btn btn-large btn-methyl" %>
    <% end %>
  </div>
</div>

我的控制器位于/app/controllers/reset_password_controller.rb:

class ResetPasswordController < ApplicationController

  def new
    @user = User.new
  end

  def show
  end

  def create
    @user = User.find_by_email(params[:email].downcase)
    user.send_password_reset if user
    redirect_to root_path, notice: "Email sent with password reset instructions."
  end

  def edit
    @user = User.find_by_password_reset_token!(params[:id])
  end

  def update
    @user = User.find_by_password_reset_token!(params[:id])
    if @user.reset_password_sent_at < 2.hours.ago
      redirect_to_new password_reset_path, alert: "Reset password request has expired."
    elsif @user.update_attributes(params[:user])
      redirect_to root_path, notice: "Password has been reset!"
    else
      render :edit
    end
  end
end

我的路线位于/config/routes.rb:

Methylme::Application.routes.draw do

  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  resources :reset_password

  root to: 'static_pages#home'

  match '/signup', to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

  match '/help', to: 'static_pages#help'
  match '/about', to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'
.
.
.
end

最后,$ rake routes 报告如下:

               users GET    /users(.:format)                   users#index
                     POST   /users(.:format)                   users#create
            new_user GET    /users/new(.:format)               users#new
           edit_user GET    /users/:id/edit(.:format)          users#edit
                user GET    /users/:id(.:format)               users#show
                     PUT    /users/:id(.:format)               users#update
                     DELETE /users/:id(.:format)               users#destroy
            sessions POST   /sessions(.:format)                sessions#create
         new_session GET    /sessions/new(.:format)            sessions#new
             session DELETE /sessions/:id(.:format)            sessions#destroy
reset_password_index GET    /reset_password(.:format)          reset_password#index
                     POST   /reset_password(.:format)          reset_password#create
  new_reset_password GET    /reset_password/new(.:format)      reset_password#new
 edit_reset_password GET    /reset_password/:id/edit(.:format) reset_password#edit
      reset_password GET    /reset_password/:id(.:format)      reset_password#show
                     PUT    /reset_password/:id(.:format)      reset_password#update
                     DELETE /reset_password/:id(.:format)      reset_password#destroy
                root        /                                  static_pages#home
              signup        /signup(.:format)                  users#new
              signin        /signin(.:format)                  sessions#new
             signout DELETE /signout(.:format)                 sessions#destroy
                help        /help(.:format)                    static_pages#help
               about        /about(.:format)                   static_pages#about
             contact        /contact(.:format)                 static_pages#contact

提前感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails ruby routes forgot-password


    【解决方案1】:

    我认为您不想在密码重置视图中链接到 new_reset_password_path (new),而是链接到 reset_password_path (create),确实发送重置密码电子邮件。

    如果您的路由不符合您的预期(例如,create 路由没有关联的 xxx_path 名称),您应该单独声明它们,使用

    post '/reset_password', to: 'reset_password#create', as: 'send_reset_password' # for example
    ...
    

    【讨论】:

    • 感谢您的回复@alestanis!我想我听从了您的建议 - 我将 &lt;%= form_for @user, url: new_reset_password_path do |f| %&gt; 更改为 &lt;%= form_for @user, url: reset_password_path do |f| %&gt;。但现在我收到No route matches {:action=&gt;"show", :controller=&gt;"reset_password"} 路由错误。希望我在这里不会太密集......
    • 当我回复时,关于显式路由的最后一点没有出现。现在我看到我已经添加了post '/reset_password', to: 'reset_password#create', as: 'send_reset_password'get '/reset_password', to: 'reset_password#new', as: 'reset_password'。现在我收到一条错误消息:undefined method downcase' for nil:NilClass, which is referring to the @user = User.find_by_email(params[:email].downcase)` 我的控制器的行。我不太确定如何在这里定义参数的类。再次感谢您的帮助。
    • @DTrain 好吧,至少你现在正在进入控制器 :) 我不知道 :email 参数,如果你填写了电子邮件字段,你的代码似乎对表单是正确的...
    • 啊哈!有效!最后一个技巧是在create 方法的第二行和第三行中的user 变量前面添加@。你不知道这是一种怎样的解脱。谢谢!
    【解决方案2】:

    这是 Ryan 最好的身份验证教程之一,

    http://railscasts.com/episodes/250-authentication-from-scratch-revised

    【讨论】:

    • 感谢您的回复!我设法解决了这个问题,但我肯定会牢记 Railscast 的付费部分,以备将来使用。
    猜你喜欢
    • 2016-12-09
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多