本文介绍了导轨和设计:两步确认参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的困境和戏剧的延续...

贯穿:

  • 我可以输入我的电子邮件进行注册!
  • 我可以点击确认链接!
  • 我被带到正确的页面进行确认:

  • 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"}
    

    问题在第 10 行:

    class ConfirmationsController < Devise::ConfirmationsController
      def show
        @user = User.find_by_confirmation_token(params[:confirmation_token])
        if [email protected]?
          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
    

    这是我的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).

    如果您需要,我很乐意为您提供更多信息.为了方便起见,您能否详细描述一下您的答案--我是 Rails 新手!

    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!

    推荐答案

    问题似乎在于使用 User.find(params[:user][:confirmation_token]) 查找用户.

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

    这是因为 find() 方法将通过 ID 查找用户.使用不同的方法通过确认令牌查找用户应该返回正确的用户.在 show 动作中您已经使用过一次该方法.

    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!

    这篇关于导轨和设计:两步确认参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-14 04:49