- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在研究 Michael Hartl Railstutorial.org - 第 10 章 - 密码重置。我在开发环境(Cloud9)配置了邮件,邮件就发了。一切正常,直到用户单击系统发送的电子邮件。它不会被重定向到密码重置表单,而是转到根目录。将它与书本进行比较,即使使用其他成员的相同项目代码 - 也找不到任何故障。很可能它是非常简单的东西,但我找不到它:(感谢任何帮助...
生成的链接:
http://rails-tutorial-maxviskov.c9users.io/password_resets/7k827F5v0KPWj8kuWEvJOg/edit?email=max.viskov%40felicity-world.com
路线.rb:
Rails.application.routes.draw do
get 'sessions/new'
get 'users/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
用户模型
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Password.create(string, cost: cost)
end
def User.new_token
SecureRandom.urlsafe_base64
end
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(self.remember_token))
end
# Returns true if the given token matches the digest.
def authenticated?(attribute, token)
digest = send("#{attribute}_digest")
return false if digest.nil?
BCrypt::Password.new(digest).is_password?(token)
end
def forget
update_attribute(:remember_digest, nil)
end
# Returns the hash digest of the given string.
def self.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def self.new_token
SecureRandom.urlsafe_base64
end
class << self
# Returns the hash digest of the given string.
def digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def new_token
SecureRandom.urlsafe_base64
end
end
# Activates an account.
def activate
update_attribute(:activated, true)
update_attribute(:activated_at, Time.zone.now)
end
# Sends activation email.
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
# 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
def password_reset_expired?
reset_sent_at <2.hours.ago
end
private
def downcase_email
self.email = email.downcase
end
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
end
密码重置 Controller
class PasswordResetsController < ApplicationController
before_action :get_user, only: [:edit, :update]
before_action :valid_user, only: [:edit, :update]
before_action :check_expiration, only: [:edit, :update]
def new
end
def create
@user = User.find_by(email: params[:password_reset][:email].downcase)
if @user
@user.create_reset_digest
@user.send_password_reset_email
flash[:info] = "Email sent with password reset instructions"
redirect_to root_url
else
flash.now[:danger] = "Email address not found"
render 'new'
end
end
def edit
end
def get_user
@user = User.find_by(email: params[:email])
end
# Confirms a valid user.
def valid_user
unless (@user && @user.activated? &&
@user.authenticated?(:reset, params[:id]))
redirect_to root_url
end
end
def update
if params[:user][:password].empty?
@user.errors.add(:password, "can't be empty")
render 'edit'
elsif @user.update_attributes(user_params)
log_in @user
flash[:success] = "Password has been reset."
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:password, :password_confirmation)
end
# Before filters
def get_user
@user = User.find_by(email: params[:email])
end
# Confirms a valid user.
def valid_user
unless (@user && @user.activated? &&
@user.authenticated?(:reset, params[:id]))
redirect_to root_url
end
end
# Checks expiration of reset token.
def check_expiration
if @user.password_reset_expired?
flash[:danger] = "Password reset has expired."
redirect_to new_password_reset_url
end
end
end
密码重置HTML
<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) %>
<!--reset_password_token: @token -->
<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>
/app/views/password_resets/edit.html.erb
<% 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>
Rake 路线结果:
Prefix Verb URI Pattern Controller#Action
sessions_new GET /sessions/new(.:format) sessions#new
users_new GET /users/new(.:format) users#new
root GET / static_pages#home
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
signup GET /signup(.:format) users#new
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
password_resets POST /password_resets(.:format) password_resets#create
new_password_reset GET /password_resets/new(.:format) password_resets#new
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset PATCH /password_resets/:id(.:format) password_resets#update
PUT /password_resets/:id(.:format) password_resets#update
最佳答案
完成。刚刚重新创建了所有文件,对照这本书进行了检查,它有效
关于 ruby 。密码重置链接未重定向到表单 (Railstutorial.org),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35304879/
app/helpers/sessions_helper.rb module SessionsHelper def sign_in(user) cookies.permanent[
在 rubyrailstutorial.org 之后达到内存限制 你好, 我正在关注 Ruby Rails Tutorial 上的教程我正在使用 Cloud9 IDE但是,在运行本教程时,我不断收
我正在研究 Michael Hartl Railstutorial.org - 第 10 章 - 密码重置。我在开发环境(Cloud9)配置了邮件,邮件就发了。一切正常,直到用户单击系统发送的电子邮件
在第 7 章中,我使用 获得以下输出 --- !ruby/hash-with-ivars:ActionController::Parameters elements: controller: st
在 Ruby on Rails 3 教程的 6.2.4 节中,Michael Hartl 描述了一个关于检查电子邮件地址唯一性的警告:如果两个相同的请求及时接近,请求 A 可以通过验证,然后 B 通过
我正在尝试关注 Rails Tutorial,但我的 rspec 失败了,但在浏览器中,该应用程序可以按需运行。那是当我登录(在浏览器中)并转到“http://localhost:3000/users
我正在尝试关注 railstutorial.org,目前在第 7 章,您开始使用工厂:http://railstutorial.org/chapters/modeling-and-viewing-us
我正在尝试让错误闪现出现在 railstutorial.org 示例应用程序中。到目前为止,这就是我所做的。谁能帮我找出错误: 我的 RSPEC 测试失败: 1) SessionsController
在11.2.5部分在 Railstutorial(.org) 的 Rails 3.2 版本中,我无法让所有的https://relationships_controller_spec.rb 测试通过,
我是 Rails 的新手,所以请原谅以下菜鸟问题并提前致谢... 我正在关注 rails tutorial然而,一旦我到达第 9.12 节,我们将忘记逻辑添加到 sessions_helper.rb,
在阅读 Michael Hartl 的 railstutorial.org 时,我进入了第 8 章(特别是 8.2.3)。当前的问题是实现一个 session 以保持用户在多个 View 中登录,但是
在railstutorial中,作者为什么选择使用这个(代码 list 10.25): http://ruby.railstutorial.org/chapters/updating-showing-
这个想法是为了让管理员用户无法 self 毁灭。我写了以下测试: describe "as admin user" do let(:admin) { FactoryGirl.create(:adm
这个想法是为了让管理员用户无法 self 毁灭。我写了以下测试: describe "as admin user" do let(:admin) { FactoryGirl.create(:adm
我正在学习 http://ruby.railstutorial.org 上的教程 具体来说,第 9 章 (9.2.3) http://ruby.railstutorial.org/chapters/u
我没有从 Hartl 的 Rails 教程中得到以下练习的答案: By replacing the question marks in Listing 4.10 with the appropriat
我正在关注 rails 教程并被困在第 1.4.3 章 Bitbucket 上。 https://www.railstutorial.org/book/beginning#sec-bitbucket
我已完成 Chapter 6 of railstutorial但我所有的 User model specs在我添加 password 后不久就开始失败& password_confirmation有以
我在 Section 11.3.1的 Rails 教程,所有测试都在此之前通过。之后,主页(有微博提要)因以下错误而中断: PG::Error: ERROR: invalid input synta
示例代码: assert_select 'div#' assert_select 'div.' 这会产生:弃用警告:由于无效的 css 选择器,断言未运行。 ] 16% 时间:00:00:01,预计到
我是一名优秀的程序员,十分优秀!