Search code examples
ruby-on-railsmailer

Mailer uses same controller code as ___ controller


So I want to mail out a report that has class variables regularly defined by the controller. However in the mailer I have to define them again? Is there a way to not repeat this? I've thought about moving it more to the model but in the end it's still the same amount of definitions.


Solution

  • You don't have to define them again, you can pass them from the controller as parameters to the mailer method.

    Assuming you need @ivar1, @ivar2 and @ivar3 in your mail all you have to do is run this from the controller:

    MyMailer.some_email(@ivar1, @ivar2, @ivar3).deliver
    

    EDIT:

    Having a second look at your question (and my answer), I think I may have misunderstood it in first place.

    In my solution you will still have to re-define the instance variables. The values are correctly passed but you will still have to:

    def some_email(v1, v2, v3)
      @ivar1 = v1
      @ivar2 = v2
      @ivar3 = v3
    end
    

    There is no way to use the same instance variable defined in the controller.