views:

46

answers:

1

I have a form_for in a template new.html.erb where I do the normal save to the DB and then an e-mail on success. I want the mailer to send the same new.html.erb as the body and pass the model so that the form is fully populated. I'm getting the following error:

undefined method `protect_against_forgery?' for #<#<Class:0x000000049d7110>:0x000000049d4690>

On the line immediately after the form_for tag (since it's injecting the auth token tag I think). Is there a way I can circumvent this so that I can re-use the template in the mailer?

Here's what the mailer code looks like

class MaintenanceMailer < ActionMailer::Base
  helper :application  

  def request_email(maintenance)
    mail :to => maintenance.community.email, :subject => "Maintenance" do |format|
      format.html { render :layout => 'default', :template => 'maintenance/new' }
    end    
  end
end  
A: 

Add this to a helper that only your mailer template uses:

    def protect_against_forgery?
      false
    end

But make sure that the receiving controller skips the verify_authenticity_token, and that session jacking is ok for whatever that form carries.

Sam C
I tried that earlier and it gives me another error:ActionController::InvalidAuthenticityTokenI want that action to verify authenticity on a normal page request for the form. Do I need to send something to the controller once I'm in the mailer? All it should have to do is now render that "new" template w/o going back to the controller.
jpfuentes2
Yes, that's the skips "verify_authenticity_token" part. The syntax to do that in your controller (at the top) is: skip_before_filter :verify_authenticity_token, :only => [:my_form_action]
Sam C