2017-08-26 176 views
0

我在設置重發激活電子郵件時遇到問題。當用戶創建一個帳戶時,電子郵件發送完美,但是當他們嘗試重新發送激活電子郵件時,我會收到錯誤消息。預先感謝所有的幫助。Rails重發電子郵件激活

這是在會話#resend_activation錯誤

的ActionController :: UrlGenerationError

沒有路由匹配{:行動=> 「編輯」,:控制器=> 「account_activations」, :電子郵件=> 「[email protected]」:ID =>零},可能無法比擬 約束:[:ID]

這是線其高亮在account_activation.html.erb

<%= link_to edit_account_activation_url(@user.activation_token, email: @user.email) do %> 

的routes.rb

resources :account_activations, only: [:edit] 

get '/resend_page', to: 'sessions#resend_account_activation_page' 
post 'resend_activation', to: 'sessions#resend_activation' 

resend_account_activation_page.html.erb

<div class="FormBody"> 
     <h1 style="margin-bottom:30px">Account Activation</h1> 

     <p> If you need the activation email to be resent enter the email you signed up with and hit the resend button. </p> 
     <%= form_for(:sessions, url: resend_activation_path) do |f| %> 

      <div> 
       <%= f.text_field :email, class: "Input", placeholder: "Email"%> 
      </div> 

      <%= f.submit "Resend", class: "SignUpButton", data: {toggle: "tooltip"}, title: "Resend" %> 
     <% end %> 
    </div> 

sessions_controller.rb

def resend_activation 
    if @user = User.find_by_email(params[:sessions][:email]) 
     @user.send_activation_email 
     flash[:success] = "Activation email resent!" 
     redirect_to root_url 
    else 
     flash.now[:danger] = 'Email is not asociated with any account, please sign up first.' 
     redirect_to root_url 
    end 
    end 

user.rb

def activate 
    update_columns(activated: true, activated_at: Time.zone.now) 
    end 

    def send_activation_email 
    UserMailer.account_activation(self).deliver_now 
    end 

account_activations_controller.rb

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 root_url 
    else 
     flash[:danger] = "Invalid activation link" 
     redirect_to root_url 
    end 
    end 
end 

user_mailer.rb

def account_activation(user) 
    @user = user 
    mail to: user.email, subject: "Account Activation" 
end 

回答

0

我想通了我忘了我在發送電子郵件之前必須調用create_activation_digest,以便創建一個新的令牌。謝謝您的幫助!

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

edit_account_activation_urlaccount_activation.html.erb需要一個用戶ID,所以你應該改變它:

<%= link_to edit_account_activation_url(@user.id) do %> 

然後你就可以通過ID檢索用戶在account_activations_controller.rb

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