views:

302

answers:

3

Hi I am trying to figure out what I am doing wrong with ActionMailer. I need to be able to pass more than one argument to a mailer but continue to receive the error "wrong number of arguments (1 for 2)".

My code is: soldier_controller

def create
  @soldier = Soldier.new(params[:soldier])
  @battalion = Battalion.find(params[:battalion_id])
  @company = Company.find(params[:company_id])
  @frg = @company.users.find_by_position('FRG Leader')
respond_to do |format|
  if @soldier.save
    flash[:notice] = 'Soldier was successfully created.'
    format.html { redirect_to battalion_company_soldier_path(@battalion, @company, @soldier)}
    format.xml  { render :xml => @soldier, :status => :created, :location => @soldier }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @soldier.errors, :status => :unprocessable_entity }
    end
  end
end

soldier_mailer.rb:

class SoldierMailer < ActionMailer::Base

def welcome_email(soldier, primary)  
  recipients soldier.primary.email
  from "[email protected]"  
  subject "Welcome to the Unit"  
  sent_on Time.now 
  body 
end

soldier_observer.rb:

 def after_create(soldier, primary)  
   SoldierMailer.deliver_welcome_email(soldier, primary)  
 end

Basically I need to send this same email to several different recipients whose email addresses are in models all associated with the Soldier model. I have no problem sending an email to when there is just one parameter in the method. If I add more than one it throws that error.

I would appreciate any help or guidance.

Thanks.

A: 

Maybe I don't totally understand your predicament, but is there anything wrong just sending something different to your deliver_welcome_email method?

Maybe you could send an array or a hash to the method with your multiple recipients. From reading the rails documentation it seems that these action mailer methods only take one argument.

Also I'm a little confused about why you need to send both of those parameters to your mailer. It looks like you only used soldier.

+2  A: 

after_create method can only take a model as parameter. if primary is a field of soldier, you should do something like

def after_create(soldier)
  SoldierMailer.deliver_welcome_email(soldier, soldier.primary) 
end
ez
A: 

Try this:

class SoldierMailer < ActionMailer::Base

  def welcome_email(soldier)  
    recipients soldier.email_addresses
    from "[email protected]"  
    subject "Welcome to the Unit"  
    sent_on Time.now 
  end
end

With this in your Soldier model: I'm assuming there is an association that contains more email addresses you want to use.

class Soldier

  has_many :users

  def email_address
    # Results in ['[email protected]', '[email protected]', 'user2@abc'.com]
    ([primary] + users.collect { |u| u.email }).flatten
  end
end

The key is that 'recipients' in ActionMailer can take an array of email addresses. Hope this helps.

Ariejan
great this is a huge help! thanks so much.