Rails & Devise: Two-step confirmation params error

2019-08-07 03:07发布

问题:

This is a continuation of my woes and drama ...

Run through:

  • I can enter my email to register!
  • I can click the confirmation link!
  • I'm taken to the correct page to confirm:

    http://localhost:3000/user/confirmation?confirmation_token=jxOZQnyixE1PvnrptnYO

  • Here's the problem...once I submit the form, I get this error:

ActiveRecord::RecordNotFound in ConfirmationsController#confirm_account

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"qUk6EDoR6N+V0h5O/jLKNZtl0hiaN/g9Gd5YdI2QhIU=",
 "user"=>{"confirmation_token"=>"jxOZQnyixE1PvnrptnYO",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]"},
 "commit"=>"Confirm Account"}

The problem is in line 10:

class ConfirmationsController < Devise::ConfirmationsController
  def show
    @user = User.find_by_confirmation_token(params[:confirmation_token])
    if !@user.present?
      render_with_scope :new
    end
  end

  def confirm_account
    @user = User.find(params[:user][:confirmation_token])
    if @user.update_attributes(params[:user]) and @user.password_match?
      @user = User.confirm_by_token(@user.confirmation_token)
      set_flash_message :notice, :confirmed      
      sign_in_and_redirect("user", @user)
    else
      render :action => "show"
    end
  end
end

Here's my show.html.erb

<%= form_for(resource, :url => confirm_account_path) do |f| %>
    <%= f.label :email %>
    <%= @user.email %>
    <%= f.hidden_field :confirmation_token %>
    <%= f.label :password %>
    <%= f.password_field :password %>
    <%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation %>
    <%= f.submit 'Confirm Account' %>
    <%= link_to 'Home', root_url %>
    <%= render :partial => 'devise/shared/links' %>
<% end %>

I've been crying about this for a week...I really hope this is a moronic mistake on my part (and at the same time I don't).

I'll be happy to provide you with more information if you need it. For my convenience, could you describe your answers throughly--I'm a rails newbie!

回答1:

The problem seems to be in finding the user with User.find(params[:user][:confirmation_token]).

This is because the find() method will look for a user by ID. Using a different method to find the user by confirmation token should return the right user. In the show action you already used this method once.

Hopefully this is the last problem!