【问题标题】:Rails & Devise: Two-step confirmation params errorRails & Devise:两步确认参数错误
【发布时间】:2011-09-30 11:38:51
【问题描述】:

这是我woes and drama的延续...

贯穿:

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 !@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

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

我已经为此哭了一个星期...我真的希望这是我的一个愚蠢的错误(同时我没有)。

如果您需要,我很乐意为您提供更多信息。为方便起见,您能否完整地描述您的答案——我是一名 Rails 新手!

【问题讨论】:

    标签: ruby-on-rails rails-activerecord registration confirmation


    【解决方案1】:

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

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

    希望这是最后一个问题!

    【讨论】:

    • 我有点理解。我需要找到一个用户,其中confirmation_token => :confirmation_token 之类的,对吗???就像我说的,我是新手。如果你能扩展,我会很高兴。除了“查找”之外,我还有其他方法可以使用吗?
    • 是的!我可以告诉你,但我认为你应该自己找到它。使用与 show 操作相同的方法,即 @user = find...
    • 哦……我的……天哪。我花了这么长时间。我想这就是你学习时的方式。 :) 非常感谢!
    • 好,恭喜!为了将来参考,find_by_something() 方法可用于使用 ActiveRecord 的任何记录。 api.rubyonrails.org/classes/ActiveRecord/Base.html
    猜你喜欢
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 2016-02-17
    相关资源
    最近更新 更多