I'm trying to create an email which is sent when a Parent (equivalent to a User) creates a new Baby. I've been trying lots of things but can't quite figure out what I'm doing wrong.
Parents have many babies. Babies belong to parents.
I'm sending the email to parents by adding
NewBabyMailer.new_baby_email(@parent).deliver
to this block of code in the babies_controller.rb
def create
@baby = Baby.new(params[:baby])
@parent = current_parent
if @baby.save
# @baby = Baby.find(params[:id])
NewBabyMailer.new_baby_email(@parent).deliver
redirect_to(root_url(:host => with_subdomain(@baby.subdomain)), :notice => 'Your baby was successfully added. Share this page with people and tell them to sign up to be notified when your baby is born')
else
render :action => "new"
end
end
In my new newbabymailer.rb mailer I have
class NewBabyMailer < ActionMailer::Base
default :from => "blah"
default :bcc => "blah"
@parent = parent
@baby = Baby.find_by_parent_id
# @baby = Baby.where(:parent_id = :params[:id])
def new_baby_email(parent)
mail(:to => parent.email, :subject => "Thanks for adding a baby", :bcc => "blah")
end
end
But when I call things like
<%= @baby.name.titleize %>
in my newbabyemail.html.erb, I get
undefined method `baby' for nil:NilClass
Any suggestions as to what I am doing wrong? I'm sure it's something really simple, but it's stumped me for the whole day.
Cheers Paul