0

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

Paul
  • 51
  • 6
  • I think your baby table has a parent_id column and you save a baby without assigning a parent to it. – cristian Dec 10 '11 at 13:08
  • The baby table does indeed have a parent_id column on it. What do you suggest I change? – Paul Dec 10 '11 at 13:09
  • Look at your baby records into your database. Be sure each row has endeed it's coresponding parent_id set. Does params[:baby] contains a parent_id key? – cristian Dec 10 '11 at 13:13
  • I just looked it up and babies were being created without a parents_id key. So I added @baby.parent = current_parent to the create method. I'm still not able to use that in the emailer though, but at least Babies are being saved with Parents. Thanks for picking that up. – Paul Dec 10 '11 at 13:20
  • Just 2 more things. 1. `Baby.find_by_parent_id` could return more than one baby and 2. In NewBabyMailer why do you fetch and set @baby? can't you use directly parent.babies.last in newbabyemail.html instead of @baby? – cristian Dec 10 '11 at 13:29

1 Answers1

1

You are trying to set instance variables at the class level instead of the method that actually runs. Rails defines a Module#parent method which is why @parent = parent doesn't raise an exception.

Instead of

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

It should be:

class NewBabyMailer < ActionMailer::Base
  default :from => "blah"
  default :bcc => "blah"

  def new_baby_email(parent)
    @parent = parent
    @baby = Baby.find_by_parent_id(parent)
    mail(:to => parent.email, :subject => "Thanks for adding a baby", :bcc => "blah")
  end
end
Andrew France
  • 4,758
  • 1
  • 25
  • 27