gpt4 book ai didi

ruby-on-rails - 在 Hartl 教程之后向 Rails 应用程序添加密码重置功能

转载 作者:太空宇宙 更新时间:2023-11-03 16:31:15 27 4
gpt4 key购买 nike

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

这是细节:

我的密码重置 View 位于 /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>

我的 Controller 位于 /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

预先感谢您的帮助!

最佳答案

我认为您不想在密码重置 View 中链接到 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
...

关于ruby-on-rails - 在 Hartl 教程之后向 Rails 应用程序添加密码重置功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15465047/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com