代码之家  ›  专栏  ›  技术社区  ›  jrocc

Rails重新发送电子邮件激活

  •  1
  • jrocc  · 技术社区  · 7 年前

    这就是错误所在

    ActionController::UrlGenerationError in Sessions#resend_activation

    :电子邮件=>"TestEmail@gmail.com“,:id=>nil},可能不匹配 约束:[:id]

    这是account_激活中突出显示的行。html。erb公司

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

    resources :account_activations, only: [:edit]
    
    get '/resend_page', to: 'sessions#resend_account_activation_page'
    post 'resend_activation', to: 'sessions#resend_activation'
    

      <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\u控制器。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
    

    使用者rb型

      def activate
        update_columns(activated: true, activated_at: Time.zone.now)
      end
    
      def send_activation_email
        UserMailer.account_activation(self).deliver_now
      end
    

      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
    

    def account_activation(user)
      @user = user
      mail to: user.email, subject: "Account Activation"
    end
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   jrocc    7 年前

    我发现我忘了在发送电子邮件之前必须调用create\u activation\u 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
    
        2
  •  0
  •   Alejandro Escobar    7 年前

    edit_account_activation_url 在里面 account_activation.html.erb

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

    然后您可以在 account_activations_controller.rb id为:

      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