Solution

多亏了史蒂文·哈曼这张gist张表格,我才得以成功.

module Features
  module MailHelpers

    def last_email
      ActionMailer::Base.deliveries[0]
    end

    # Can be used like:
    #  extract_token_from_email(:reset_password)
    def extract_token_from_email(token_name)
      mail_body = last_email.body.to_s
      mail_body[/#{token_name.to_s}_token=([^"]+)/, 1]
    end

  end
end

我将文件devise_mail_helpers.rb添加到与功能规范相同的文件夹中,并编写了此规范.

require 'devise_mail_helpers.rb'
include Features
include MailHelpers
describe "PasswordResets" do
  it "emails user when requesting password reset" do
    user = FactoryGirl.create(:user)
    visit root_url
    find("#login_link").click
    click_link "Forgot your password?"
    fill_in "Email", :with => user.email
    click_button "Send instructions"
    current_path.should eq('/users/sign_in')
    page.should have_content("You will receive an email with instructions about how to reset your password in a few minutes.")
    last_email.to.should include(user.email)
    token = extract_token_from_email(:reset_password) # Here I call the MailHelper form above
    visit edit_password_url(reset_password_token: token)
    fill_in "user_password", :with => "foobar"
    fill_in "user_password_confirmation", :with => "foobar1"
    find('.signup_firm').find(".submit").click
    page.should have_content("Password confirmation doesn't match Password")
  end
 end

这会考虑到规格,让它在浏览器中工作看看下面Dave的答案.

Original Question

在我的rails 4应用程序中,我将Desive升级到3.1并运行了rails s次,然后我得到了以下结果:

`raise_no_secret_key': Devise.secret_key was not set. 
 Please add the following to your Devise initializer: (RuntimeError)
 config.secret_key = '--secret--'

我把密钥加到了设计初始值设定项上.

在此之后,当我试图重置密码时,我会出现以下错误

Reset password token is invalid

邮件中发送的令牌似乎不正确.其他一切都在起作用.我像一把温热的刀一样进进出出.

Update

现在我猜这一定是功能规范中reset_password_token的加密:

user = FactoryGirl.create(:user, 
 :reset_password_token => "something", 
 :reset_password_sent_at => 1.hour.ago)
visit edit_password_url(user, :reset_password_token => 
  user.reset_password_token)
fill_in "user_password", :with => "foobar"
click_button "Change my password"
page.should have_content("Password confirmation doesn't match Password")

发生的错误是:

Failure/Error: page.should have_content
("Password confirmation doesn't match Password")        
expected to find text "Password confirmation doesn't match Password" in 
"Reset password token is invalid"

你知道我错过了什么吗?

推荐答案

你在不久前 comments 过my similar question,我找到了一个可能对你也有帮助的答案.

升级到Desive 3.1.0后,我有一段时间没碰过它了.根据this blog post,你需要将你的Desive mailer改为@token而不是原来的@resource.confirmation_token.

app/views/<user>/mailer/reset_password_instructions.html.erb中找到这个,并将其更改为:

<p>Hello <%= @resource.email %>!</p>
<p>Someone has requested a link to change your password, and you can do this through the link below.</p>
<p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @token) %></p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>

这将修复您遇到的任何基于令牌的确认问题.这可能也会修复任何解锁或确认令牌问题.

Ruby-on-rails相关问答推荐

使用Rails和RSpec,有没有办法查看操作真正将您带到哪个页面?

是否从已转换为字符串的数组中提取符号?

刺激不添加侦听器以搜索表单输入

在 Datatable Ajax 调用中通过 Ajax 更新 div

将日期时间转换为月、日和年?

Rails 100% 新手问题 - send() 方法

有没有办法在模型而不是视图中使用复数()?

Rails ActiveRecord 查询日期范围

Rails 3:验证组合值

在具有唯一约束的关联中使用 Rails 中的 factory_girl.得到重复的错误

获取磁盘上 ActiveStorage 文件的路径

我们如何在 Rails Mailer 中设置邮箱发件人的名称?

加载常量时自动加载常量时检测到循环依赖

强参数需要多个

你如何覆盖 form_for 助手中的类名?

使用 PostgreSQL 的模式和 Rails 创建多租户应用程序

从任何编码强制字符串为 UTF-8

在创建控制器和模型之后(仅)创建 Ruby on Rails 视图

在 GIT 中处理 Rails db/schema.rb 文件的正确方法是什么?

在 Rails 中显示主机名和数据库名