samedi 25 avril 2015

RoR on Heroku - Emails not getting confirmed after clicking confirm link


Okay, so I created an app with registration system. I successfully deployed the app on Heroku. I can register a user and the confirm links go to their inbox. But when they click the link, it shows "Something went wrong..." on heroku.

the confirm link is like this: http://ift.tt/1bEOsN5

Would really appreciate if someone would give a reply!

My users controller:

def create


 @user = User.create(user_params)

  if @user.save
    UserMailer.registration_confirmation(@user).deliver
    flash[:success] = "Please confirm email"
    redirect_to root_url
  else
    flash[:error] = "Something went wrong..."
    render 'new'
  end
end

def confirm_email
user = User.find_by_confirm_token(params[:id])
if user
  user.email_activate
  flash[:success] = "Welcome to the Sample App! Your email has been confirmed.
  Please sign in to continue."
  redirect_to signin_url
else
  flash[:error] = "Sorry. User does not exist"
  redirect_to root_url
end

private

def user_params
  params.require(:user).permit(:name, :username, :email, :password, :password_confirmation, :avatar_url)
end

end

My users.rb

require 'digest/md5'

class User < ActiveRecord::Base

before_create :confirmation_token

before_validation :prep_email

    def create_avatar_url
        self.avatar_url = "http://ift.tt/RKx790"
    end

has_secure_password

validates :name, presence: true
validates :username, uniqueness: true, presence: true
validates :email, uniqueness: true, presence: true, format: { with: /\A[\w.+-]+@([\w]+.)+\w+\z/ }

def email_activate
    self.email_confirmed = true
    self.confirm_token = nil
    save!(:validate => false)
end

private

def prep_email
    self.email = self.email.strip.downcase if self.email
end

def confirmation_token
  if self.confirm_token.blank?
      self.confirm_token = SecureRandom.urlsafe_base64.to_s
  end
end

The sessions controller:

    def create
user = User.find_by_username(params[:username])
if user && user.authenticate(params[:password])
  if user.email_confirmed
    session[:user_id] = user.id
    redirect_to root_url, notice: "Logged in!"
else
    flash.now[:error] = 'Please activate your account by following the 
    instructions in the account confirmation email you received to proceed'
end


else
    flash.now[:error] = "Invalid user/pass"
  end
  end

  def destroy
    session.delete(:user_id)
    redirect_to root_url, notice: "Logged out."
  end

The app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base

default :from => "no-reply@domain.com"

def registration_confirmation(user)
    @user = user
    mail(:to => "#{user.name} <#{user.email}>", :subject => "Registration Confirmation")
end

end

Also added this to my routes file

     resources :users do
    member do
      get :confirm_email
    end

 end

Also added a text.erb file in my app/views/user_mailer/registration_confirmation.text.erb

Hi <%= @user.name %>,

Thanks for registering! To confirm your registration click the URL below.

<%= confirm_email_user_url(@user.confirm_token) %>

SMTP settings in developments.rb

      # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.default_url_options = {:host => "localhost:3000"}
  config.action_mailer.delivery_method = :smtp 
  config.action_mailer.smtp_settings = { 
    address: 'smtp.gmail.com', 
    port: 587, 
    domain: 'gmail.com', 
    user_name: 'mygmail@gmail.com', 
    password: 'mypassword', 
    authentication: 'plain', 
    enable_starttls_auto: true
   }

I tried to tail my heroku logs (heroku logs --tail) and I was monitoring the logs as I was registering. It gave me a 500 status error on my logs.

2015-04-25T08:06:21.545018+00:00 heroku[web.1]: State changed from starting to up

2015-04-25T08:06:22.709790+00:00 heroku[router]: at=info method=GET path="/" host=keplarblog4.heroku
app.com request_id=d1497cc9-02f1-4b55-8f72-a2efd95faf7a fwd="117.102.24.185" dyno=web.1 connect=0ms
service=267ms status=200 bytes=2847

2015-04-25T08:06:23.174505+00:00 heroku[router]: at=info method=GET path="/less.js" host=keplarblog4
.herokuapp.com request_id=1d6643da-a068-48e0-bc7f-1fc9c18af1af fwd="117.102.24.185" dyno=web.1 conne
ct=0ms service=6ms status=304 bytes=133

2015-04-25T08:06:23.172282+00:00 heroku[router]: at=info method=GET path="/gfx/logo.png" host=keplar
blog4.herokuapp.com request_id=abd50fb2-6861-47bb-b884-6c367d35d857 fwd="117.102.24.185" dyno=web.1
connect=0ms service=8ms status=304 bytes=133

2015-04-25T08:06:23.629123+00:00 heroku[router]: at=info method=GET path="/gfx/logo-nettuts.png" hos
t=keplarblog4.herokuapp.com request_id=77932ef2-842e-453d-b12b-fc634394ff91 fwd="117.102.24.185" dyn
o=web.1 connect=0ms service=4ms status=304 bytes=133

2015-04-25T08:06:23.701511+00:00 heroku[router]: at=info method=GET path="/style.less" host=keplarbl
og4.herokuapp.com request_id=d28896bf-abef-4d8c-bb79-a6a3788435aa fwd="117.102.24.185" dyno=web.1 co
nnect=1ms service=3ms status=304 bytes=133

2015-04-25T08:06:23.627140+00:00 heroku[router]: at=info method=GET path="/gfx/frog.jpg" host=keplar
blog4.herokuapp.com request_id=6a33e5cd-c014-4e92-a60e-2d5ebeb1b866 fwd="117.102.24.185" dyno=web.1
connect=0ms service=7ms status=304 bytes=133

2015-04-25T08:06:24.014723+00:00 heroku[router]: at=info method=GET path="/gfx/bg-header.png" host=k
eplarblog4.herokuapp.com request_id=9ab6b848-e862-4632-820d-fb9e900f6b61 fwd="117.102.24.185" dyno=w
eb.1 connect=0ms service=7ms status=304 bytes=133

2015-04-25T08:06:24.027774+00:00 heroku[router]: at=info method=GET path="/gfx/bg-footer.png" host=k
eplarblog4.herokuapp.com request_id=2fcb8907-9790-4c7c-84e9-e7f00f461110 fwd="117.102.24.185" dyno=w
eb.1 connect=1ms service=3ms status=304 bytes=133

2015-04-25T08:06:24.007494+00:00 heroku[router]: at=info method=GET path="/gfx/bg.png" host=keplarbl
og4.herokuapp.com request_id=5b210759-353a-41a1-b66a-47a4f4da16d8 fwd="117.102.24.185" dyno=web.1 co
nnect=1ms service=7ms status=304 bytes=133

2015-04-25T08:06:24.353070+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=keplarb
log4.herokuapp.com request_id=8fc598ef-7e97-4873-94f1-09505a49088b fwd="117.102.24.185" dyno=web.1 c
onnect=0ms service=5ms status=304 bytes=133

2015-04-25T08:07:33.733667+00:00 heroku[router]: at=info method=POST path="/users" host=keplarblog4.
herokuapp.com request_id=d9d4685f-9aab-404a-874d-ebcd3f543573 fwd="117.102.24.185" dyno=web.1 connec
t=0ms service=3262ms status=302 bytes=1033

2015-04-25T08:07:34.035847+00:00 heroku[router]: at=info method=GET path="/" host=keplarblog4.heroku
app.com request_id=d090ea4d-20e4-4ca4-a668-ecb0831e2962 fwd="117.102.24.185" dyno=web.1 connect=0ms
service=18ms status=200 bytes=3049

2015-04-25T08:08:10.065104+00:00 heroku[router]: at=info method=GET path="/users/5gOyuWA3-rqDG8WhZMJ
YjQ/confirm_email" host=keplarblog4.herokuapp.com request_id=ee307920-1157-4037-8d89-7d6b652965be fw
d="117.102.24.185" dyno=web.1 connect=0ms service=27ms status=500 bytes=1754

Thanks in advance!


Aucun commentaire:

Enregistrer un commentaire