我是一名初级开发人员,我能够成功使用本教程(我在Rails 3中):http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic/允许用户重置密码,但我也有一个客户模型,我需要这样做,我遇到了问题(下面列出代码)。我认为错误是我的路由,但我不确定如何解决它。
的routes.rb
resources :password_resets do
get 'edit_customer'
post 'edit_customer'
end
password_resets_controller.rb
class PasswordResetsController < ApplicationController
before_filter :load_user_using_perishable_token, :only => [:edit, :update]
before_filter :load_customer_using_perishable_token, :only => [:edit_customer, :update_customer]
before_filter :require_no_user, :require_no_customer
def new
render
end
def create
@user = User.find_by_email(params[:email])
@customer = Customer.find_by_email(params[:email])
if @user
@user.deliver_password_reset_instructions!
flash[:notice] = "Instructions to reset your password have been emailed to you. " +
"Please check your email."
redirect_to new_user_session_path
elsif
if @customer
@customer.deliver_customer_password_reset_instructions!
flash[:notice] = "Instructions to reset your password have been emailed to you. " +
"Please check your email."
redirect_to new_customer_session_path
end
else
flash[:notice] = "No account was found with that email address"
render :action => :new
end
end
def edit
render
end
def edit_customer
#redirect_to edit_customer_password_resets_path
end
def update
@user.password = params[:user][:password]
@user.password_confirmation = params[:user][:password_confirmation]
if @user.save
flash[:notice] = "Password successfully updated"
redirect_to new_user_session_path
else
render :action => :edit
end
end
def update_customer
@customer.password = params[:customer][:password]
@customer.password_confirmation = params[:customer][:password_confirmation]
if @customer.save
flash[:notice] = "Password successfully updated"
redirect_to new_customer_session_path
else
render :action => :edit
end
end
private
def load_user_using_perishable_token
@user = User.find_using_perishable_token(params[:id])
unless @user
flash[:notice] = "We're sorry, but we could not locate your account." +
"If you are having issues try copying and pasting the URL " +
"from your email into your browser or restarting the " +
"reset password process."
redirect_to new_user_session_path
end
end
def load_customer_using_perishable_token
@customer = Customer.find_using_perishable_token(params[:id])
unless @customer
flash[:notice] = "We're sorry, but we could not locate your account." +
"If you are having issues try copying and pasting the URL " +
"from your email into your browser or restarting the " +
"reset password process."
#redirect_to new_customer_session_path
end
end
end
模型 user.rb模型
def deliver_password_reset_instructions!
reset_perishable_token!
UserMailer.password_reset_instructions(self).deliver
end
customer.rb模型
def deliver_customer_password_reset_instructions!
reset_perishable_token!
UserMailer.customer_password_reset_instructions(self).deliver
end
视图 password_resets /新
<% form_tag password_resets_path do %>
<p>Email:</p>
<%= text_field_tag "email" %><br />
<br />
<%= submit_tag "Reset my password" %>
<% end %>
password_resets /编辑
<% form_for @user, :url => password_reset_path, :method => :put do |f| %>
<%= f.label :password %><br />
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Change my password and log me in"%>
<% end %>
password_resets / edit_customer
<% form_for @customer, :url => password_reset_path, :method => :put do |f| %>
<%= f.label :password %><br />
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Change my password and log me in" %>
<% end %>
它非常适用于没有附加“customer”的代码。令牌生成,我可以使用相同的格式发送电子邮件:url:/ password_resets // edit_customer
但是,当我尝试拉出网址时收到错误。使用上面的代码,我收到此错误:“没有路由匹配{:action =&gt;”show“,:controller =&gt;”password_resets“}” - 我还可以看到它解释:id为id控制器(与用户的工作方式不同 - 它正确地将id解释为令牌。
我经历了很多实验:
所以我难倒了。任何帮助将不胜感激!
答案 0 :(得分:0)
根据我的解释,您收到了电子邮件,但点击该链接会出错?首先出现错误消息
"No route matches {:action=>"show", :controller=>"password_resets"}"
出现是因为您尝试在password_resets控制器中调用show操作,但未定义。你应该做的第一件事是将它从你的路线中排除。
resources :password_resets, :except => :destroy do
get 'edit_customer'
post 'edit_customer'
end
我不认为这会解决你的问题,但是拥有无处可去的路线是没有意义的。其次,请确保您没有链接到show动作的任何内容。由于当您单击电子邮件中的链接时似乎发生了错误,我认为您发送的链接不正确。
答案 1 :(得分:0)
我可以通过更新 edit_customer.rb
中的网址来解决此问题这
:url => password_reset_path
到
:url => {:action=>"update_customer", :controller=>"password_resets"}
我还必须更新 route.rb
这
resources :password_resets do
get 'edit_customer'
post 'edit_customer'
end
到
resources :password_resets, :except => [:index, :destroy, :show] do
get 'edit_customer'
put 'update_customer'
end