[Rails] actionmailer issues

Jeffrey Moss jeff at opendbms.com
Thu Jan 27 01:06:05 GMT 2005


Nolan,

Why do you use a confirmation_id field as an attr_accessor? Why not juse use 
an association? I can't see an obvious problem with your code, but I do the 
exact same thing, here's how I do it:

class ApplicationMailer < ActionMailer::Base
  @@template_root = "../app/email_templates"

  def verify_notification(user)
    @recipients         = user.email_address
    @subject            = "Account Created"
    @body["first_name"] = user.first_name
    @body["last_name"]  = user.last_name
    @body["validation_key"] = user.validation_keys.build('validation_key' => 
Digest::SHA1.hexdigest(rand.to_s),
                                                         'date_created' => 
Time.now())
    @body["validation_key"].save
    @from               = "webmaster at xxxxxxxx.com"
  end
end


<html>
<p>Dear <%= @first_name %> <%= @last_name %>,</p>

<p>Thank you for registering with xxxxxxxxxxxxxxxx</p>

<p>Please click <a 
href="http://www.xxxxxxxx.com/home_page/validate?validation_key=<%= 
@validation_key.validation_key %>">here</a>\
 to validate your account.</p>

<p>Webmaster</p>
</html>


class ValidationKey < ActiveRecord::Base
  belongs_to :user
end

class HomePageController < AbstractApplicationController
  def validate
    @key = ValidationKey.find_first( ["validation_key = '%s'", 
@params['validation_key']] )
    @key.user.validated = 1
    @key.user.save
    @messages = ["Thank you #{@key.user.login}, you are now authenticated."]
    @key.destroy
    render_action "entry"
  end

If I had to guess I'd say it was something to do with your model. If you 
still have problems try putting inspect methods everywhere for debugging 
output. My code is a little dirty, looking back I'd put the validation key 
generator in its own method.

-Jeff


----- Original Message ----- 
From: "Nolan J. Darilek" <nolan at thewordnerd.info>
To: <Rails at lists.rubyonrails.org>
Sent: Wednesday, January 26, 2005 5:13 PM
Subject: [Rails] actionmailer issues


> Thanks to those of you who assisted with my FAQ problem
> yesterday. Using the form_tag helper resolved the issues nicely.
>
> Now I'm having issues with actionmailer. Authentication works fine,
> but now I'd like to add email confirmation. I'm emailing out a random
> integer as a confirmation code, and have a Conformation model mapping
> the integers to IDs in the users table. Here's my actionmailer model:
>
> class Accounts < ActionMailer::Base
>
>  def signup_email_confirm(user)
>    @user = user
>    @recipients = @user.email
>    @from = "accounts at myapp.net"
>    @subject = "Confirm email address"
>    @body["confirmation_id"] = @user.confirmation_id
>  end
>
> end
>
> . . . The relevant code from the user model:
>
> class User < ActiveRecord::Base
>  attr_accessor :confirmation_id
> . . .
> end
>
> . . . The portion of my account_controller that sends out the mail:
>
> class AccountController < ApplicationController
>  model   :user, :confirmation
> . . .
>
>  def confirm_email
>    c = {}
>    c["user_id"] = @params["id"]
>    confirmation = Confirmation.new(c)
>    confirmation.id = rand(Time.now)
>    confirmation.save
>    @user = User.find_first("id = #{@params["id"]}")
>    @user.confirmation_id = confirmation.id
>    Accounts.deliver_signup_email_confirm(@user)
>  end
> . . .
> end
>
> and the signup_email_confirm.rhtml:
> Thanks for signing up! You're almost done.
>
> Before your account is activated, you must visit the following URL:
>
> http://myapp.net/account/confirm/<%= @confirmation_id %>
>
> Your account will then be active, and you can log in using the
> username and password you specified.
>
> Unfortunately, @confirmation_id seems to be entirely blank, because
> despite receiving the emails, there's nothing after the /confirm
> action. The confirmations are being created and look fine, they just
> aren't being included in the email.
>
> Thoughts?
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails 



More information about the Rails mailing list