0

我正在关注Ryan Bates 的 #275 Railscasts,但由于password_reset_token.

这是测试:spec/requests/password_resets_spec.rb

require 'spec_helper'

subject { page }

describe "setting a new password" do

  let(:user) { FactoryGirl.create(:user) }  
  before do
    visit new_password_reset_path
    fill_in t('user.email'), with: user.email
    click_button(t('submit.reset.password'))
    visit edit_password_reset_path(user.password_reset_token)
  end

  it { should have_title(full_title(t('page.title.password_reset.edit'))) }
end

这里是错误消息:

 Failure/Error: before { visit edit_password_reset_path(user.password_reset_token) }
 ActionController::RoutingError:
   No route matches [GET] "/password_resets/edit"

当我添加puts user.password_reset_tokenafterclick_button(t('submit.reset.password'))时,我看到字符串为零。但我不明白为什么,你能帮帮我吗?

这是new_password_reset视图:

<%= form_tag password_resets_path, :method => :post do %>
  <div class="field">
    <%= label_tag :email, t('user.email') %>
    <%= text_field_tag :email, params[:email] %>
  </div>
  <%= submit_tag t('submit.reset.password'), class: "btn btn-primary" %>
<% end %>

edit_password_reset视图:

<% provide(:title, t('page.title.password_reset.edit')) %>
<%= form_for @user, :url => password_reset_path(params[:id]) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.label :password, t('user.password') %>
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation, t('user.password_confirmation') %>
    <%= f.password_field :password_confirmation %>
  </div>
  <%= f.submit t('submit.update.password'), class: "btn btn-primary"  %></div>
<% end %>

以及new_password_reset控制器createedit方法:

def create 
  user = User.find_by_email(params[:email])
  user.send_password_reset if user
  flash[:success] = t('flash.success.reset_password.sent')
  redirect_to root_url
end 

def edit
  @user = User.find_by(password_reset_token: params[:id])
end

用户模型send_password_reset中定义:

def send_password_reset
  self.password_reset_token = Digest::SHA1.hexdigest(SecureRandom.urlsafe_base64.to_s)
  self.password_reset_sent_at = Time.zone.now
  save!(validate: false)
  UserMailer.password_reset(self).deliver
end

结果是rake routes

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
4

1 回答 1

0

您需要在通话user.reload后进行操作。click_button令牌已写入数据库,但User您示例中的实例与模型中的实例是不同的 Ruby 对象。

于 2013-10-02T15:17:36.363 回答