没有路由匹配[GET]“/ password_resets / edit”

时间:2013-10-02 07:39:31

标签: ruby-on-rails rspec token

我正在关注#275 Railscasts of Ryan Bates,但由于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_token之后添加click_button(t('submit.reset.password'))时,我看到字符串为nil。但我不明白为什么,你能帮助我吗?

以下是 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

1 个答案:

答案 0 :(得分:0)

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