简体   繁体   English

页面重定向后attr_accessor = nil?

[英]Does attr_accessor = nil after the page is redirected?

I am using Hartl's Tutorial for Account Activation 我正在使用Hartl的帐户激活教程

user.rb
attr_accessor :activation_token

def User.digest(string)
  cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                              BCrypt::Engine.cost
  BCrypt::Password.create(string, cost: cost)
end    

# Returns a random token.
def User.new_token
  SecureRandom.urlsafe_base64
end

def send_activation_email
    UserMailer.account_activation(self).deliver_now
end

def create_activation_digest
  self.activation_token  = User.new_token
  self.activation_digest = User.digest(activation_token)
end

Once the update action shown below in my UsersController is complete and the user is redirected to the root_url 完成我的UsersController中显示的更新操作后,用户将重定向到root_url

def update
  @user = User.find(params[:id])
    ...
    elsif !params[:user][:email].blank?
      if @user.authenticate(params[:user][:current_password])
        @user.update_attributes(email_user_params)
        if @user.save
          @user.create_activation_digest
          @user.deactivated
          @user.send_activation_email
          log_out
          flash[:info] = "Please check email dude"
          redirect_to root_url
        else
          flash[:danger] = "Email Update Failed"
          redirect_to edit_user_email_path(@user)
        end
     else
       flash[:danger] = "Current Password Is Incorrect"
       redirect_to edit_user_email_path(@user)
     end
   ...

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

then: 然后:

:activation_token = nil. :activation_token = nil。

Is that correct? 那是对的吗?

I am asking because there are a bunch of topics on the subject of allowing the user to request a second validation email in a separate controller action and in all of those topics the discussion is stuck on routing issues, because in the email that is sent, the :activation_token is used as :id and the error message comes up :id -> nil 我问,因为有一堆关于允许用户请求第二确认电子邮件在一个单独的控制器动作,并在所有的讨论停留在路由问题这些主题的主题的主题,因为在发送的电子邮件, :activation_token用作:id并出现错误消息:id -> nil

Edit: 编辑:

class AccountActivationsController < ApplicationController class AccountActivationsController <ApplicationController

def edit
  user = User.find_by(email: params[:email])
    if user && !user.activated? && user.authenticated?(:activation, params[:id])
      user.activate
      log_in user
      flash[:success] = "Account activated!"
      redirect_to user
    else
      flash[:danger] = "Invalid activation link"
      redirect_to root_url
    end
  end
end

 user.rb
 def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
 end

So it appears that since you are using a virtual attribute to store that token at some point in time that object is leaving scope and being reloaded or some such even that is cause you to loose context. 所以看来,由于您使用虚拟属性在某个时间点存储该令牌,该对象正在离开作用域并被重新加载,或者某些此类甚至会导致您丢失上下文。 The best bet is just to ad a column to your User table to persist it between contexts and just update it where appropriate. 最好的办法就是在您的User表中添加一列,以便在上下文之间保留它,并在适当的时候更新它。

I also met this problem. 我也遇到了这个问题。 Even though I removed "redirect_to root_url", after the code runs into a different action (in the same controller), the attr_accessor still becomes nil. 即使我删除了“redirect_to root_url”,但在代码运行到不同的操作(在同一个控制器中)之后,attr_accessor仍然变为nil。

Thus my conclusion is that the lifetime of an attr_accessor only exists within the same action. 因此,我的结论是attr_accessor的生命周期只存在于同一个动作中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM