Search code examples
ruby-on-railsrubyruby-on-rails-3.2actionmailerpartial

Ruby on Rail - Partial with local in mail


I'm trying so send an email like this

mail(:to => @user.email, :from => from_email, :subject => t('orders.invoice.email_subject'), :content_type => "text/html") do |format|
    format.html { render partial: "notifier/follow_up_email.#{I18n.locale}.html.erb", locals: {storefront: storefront, order: order} }
end

Unfortunately, rails is only looking for follow_up_email.en.html and not follow_up_email.en.html.erb - What am I doing wrong?


Solution

  • render partial: ... should not contain file extensions such as en.html.erb since they are automagically added.

    http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html

    But why do you use partials for mails, couldn't you create a proper view?

    UPDATE

    It's all described in the guide on http://guides.rubyonrails.org/v3.2.16/action_mailer_basics.html

    app/mailers/user_mailer.rb:

    class UserMailer < ActionMailer::Base
    
      default from: "no-reply@memyselfandi.com"
    
      def notification(user, storefront, order)
        @user, @storefront, @order = user, storefront, order
        I18n.locale = @user.locale    # see note below
        mail to: @user.email, subject: I18n.t('orders.invoice.email_subject')
      end
    
    end
    

    app/views/user_mailer/notification.en.html.erb:

    <html><body>
    This is a mail for <%= @user.email %> yadda yadda. Use @storefront and @order here.
    </body></html>
    

    app/views/user_mailer/notification.de.html.erb:

    <html><body>
    Das ist ein mail für <%= @user.email %> bla bla.
    </body></html>
    

    The line I18n.locale = @user.locale is only necessary if you want to send async mails e.g. when triggering rake tasks from a cronjob.