Michael Hartl第10章:密码/激活电子邮件链接

时间:2015-01-26 01:49:59

标签: ruby-on-rails ruby email-validation

我正在阅读迈克尔·哈特尔的教程,我被困在第10章的最后。当我点击“我忘了密码”时 - 电子邮件发送成功。但是,“重置密码”链接会将我引导回主页,而不是重置密码页面。

在views / password_resets / edit.html.rb

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

   <div class="row">
     <div class="col-md-6 col-md-offset-3">
       <%= form_for(@user, url: password_reset_path(params[:id])) do |f| %>

       <%= render 'shared/error_messages' %>

       <%= hidden_field_tag :email, @user.email %>

       <%= f.label :password %>
       <%= f.password_field :password, class: 'form-control' %>

       <%= f.label :password_confirmation, "Confirmation" %>
       <%= f.password_field :password_confirmation, class: 'form-control' %>

       <%= f.submit "Update password", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

寄件人/ user_mailer.rb

 class UserMailer < ActionMailer::Base
 default from: "noreply@example.com"

 def account_activation(user)
   @user = user
   mail to: user.email, subject: "Account activation"
 end

 en.user_mailer.password_reset.subject
    #
    def password_reset(user)
    @user = user
   mail to: user.email, subject: "Password reset"
  end
 end

视图/ user_mailer文件/ password_reset.html.erb

<h1>Password reset</h1>

<p>To reset your password click the link below:</p>

<%= link_to "Reset password", edit_password_reset_url(@user.reset_token,
                                                  email: @user.email) %>

<p>This link will expire in two hours.</p>

<p>
If you did not request your password to be reset, please ignore this email and
your password will stay as it is.
</p>

在models / users.rb

# Sets the password reset attributes.
def create_reset_digest
    self.reset_token = User.new_token
    update_attribute(:reset_digest,  User.digest(reset_token))
    update_attribute(:reset_sent_at, Time.zone.now)
end

# Sends password reset email. 
def send_password_reset_email
    UserMailer.password_reset(self).deliver_now
end

# Returns true if a password reset has expired.
def password_reset_expired?
    reset_sent_at < 2.hours.ago
end

配置/ routes.rb中

Rails.application.routes.draw do

get 'password_resets/new'

get 'password_resets/edit'

get 'sessions/new'

root              'static_pages#home'
get 'help'    =>  'static_pages#help'
get 'about'   =>  'static_pages#about'
get 'contact' =>  'static_pages#contact'
get 'signup'  =>  'users#new'
get 'login'   =>  'sessions#new'
post 'login'   =>  'sessions#create'
delete 'logout'  =>  'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets,     only: [:new, :create, :edit, :update]

end

如果我能提供任何其他帮助,请告诉我。

2 个答案:

答案 0 :(得分:1)

我花了好几个小时试图解决这个问题,最后发现了为什么我遇到这个问题。首先,看一下迈克尔在github上这一章的路线文件:github

我的问题很简单。我没有对我尝试重置密码的用户进行身份验证,因此我不是一个有效的用户,也无法重置密码。

答案 1 :(得分:0)

先生。哈特尔的路线如此:

Rails.application.routes.draw do
  root                'static_pages#home'
  get    'help'    => 'static_pages#help'
  get    'about'   => 'static_pages#about'
  get    'contact' => 'static_pages#contact'
  get    'signup'  => 'users#new'
  get    'login'   => 'sessions#new'
  post   'login'   => 'sessions#create'
  delete 'logout'  => 'sessions#destroy'
  resources :users
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]
end

我认为您的额外条目正在缩短预定的路线。