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-ZTREmStHas9eoWH0uzbU5w@public.gmane.org" @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?
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-XsV/KxTKYItWk0Htik3J/w@public.gmane.org" 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-BEppJtBJOObp4W7C3Lsfgg@public.gmane.org> To: <Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.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-ZTREmStHas9eoWH0uzbU5w@public.gmane.org" > @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-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Wed, Jan 26, 2005 at 06:06:05PM -0700, Jeffrey Moss wrote:> 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: >I was hesitant because I wasn''t sure whether it''d be worth storing a confirmation association with each user, especially as it''d be used only once, but I''m trying to tweak things to use associations now. Here is what I have now: class User < ActiveRecord::Base has_one :confirmation . . . end class Confirmation < ActiveRecord::Base belongs_to :user end My schema: CREATE TABLE confirmations(id integer not null primary key, user_id integer not null); CREATE TABLE users ( id integer NOT NULL PRIMARY KEY, login varchar default NULL, password varchar default NULL, email varchar not null, valid boolean default false, confirmation_id integer default 0); And my AccountController#confirm_email: def confirm_email @user = User.find_first("id = #{@params["id"]}") confirmation = @user.confirmation.build(:id => rand(Time.now)) # line 48 @user.confirmation = confirmation Accounts.deliver_signup_email_confirm(@user) end My user is created, but its confirmation_id is set to 0, and I receive the following error in my logs: NoMethodError (undefined method `build'' for nil:NilClass): /app/controllers/account_controller.rb:48:in `confirm_email'' . . . I''ve also tried creating a Confirmation, assigning its values directly then saving it, but I get errors claiming that confirmation.user_id can''t be NULL (this was when I allowed for NULLs in the schema) so it seems that something is trashing that value somewhere and I don''t know where. Any ideas about what might be wrong? Furthermore, how can I debug this? I''m trying .inspect, but it doesn''t seem to appear anywhere in my logs.
You need to get used to debugging if you are ever going to make any progress unassisted. Try using the "console" script in the scripts directory to debug this. You must print the results of inspect in order to see it. Run console, load your model with require user, then start debugging: user = User.find_first("id = 123") [enter] print user.inspect [enter] confirmation = user.confirmation.build(''id'' => rand(Time.now)) [enter] print confirmation.inspect [enter] I fixed this code for you, dont forget to call save after you "build" an object. Good luck! -Jeff ----- Original Message ----- From: "Nolan Darilek" <nolan-BEppJtBJOObp4W7C3Lsfgg@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Thursday, January 27, 2005 2:34 PM Subject: Re: [Rails] actionmailer issues> On Wed, Jan 26, 2005 at 06:06:05PM -0700, Jeffrey Moss wrote: >> 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: >> > > I was hesitant because I wasn''t sure whether it''d be worth storing a > confirmation association with each user, especially as it''d be used > only once, but I''m trying to tweak things to use associations > now. Here is what I have now: > > class User < ActiveRecord::Base > has_one :confirmation > . . . > end > > class Confirmation < ActiveRecord::Base > belongs_to :user > end > > My schema: > CREATE TABLE confirmations(id integer not null primary key, user_id > integer not null); > CREATE TABLE users ( id integer NOT NULL PRIMARY KEY, login varchar > default NULL, password varchar default NULL, email varchar not null, valid > boolean default false, confirmation_id integer default 0); > > And my AccountController#confirm_email: > def confirm_email > @user = User.find_first("id = #{@params["id"]}") > confirmation = @user.confirmation.build(:id => rand(Time.now)) # line > 48 > @user.confirmation = confirmation > Accounts.deliver_signup_email_confirm(@user) > end > > My user is created, but its confirmation_id is set to 0, and I receive > the following error in my logs: > > NoMethodError (undefined method `build'' for nil:NilClass): > /app/controllers/account_controller.rb:48:in `confirm_email'' > . . . > > I''ve also tried creating a Confirmation, assigning its values directly > then saving it, but I get errors claiming that confirmation.user_id > can''t be NULL (this was when I allowed for NULLs in the schema) so it > seems that something is trashing that value somewhere and I don''t know > where. > > Any ideas about what might be wrong? Furthermore, how can I debug > this? I''m trying .inspect, but it doesn''t seem to appear anywhere in > my logs. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails