Michael Fehr
Michael Fehr

Reputation: 396

Rails 4 using partials in a mailer view

I have here a problem with partials in a view of an ActionMailer. They just do not work - and I do not know, if I do something wrong, this is a bug or this is not supported at all.

This should be working as this would be needed to have also DRY code in the mailer views (. From my testings, I see, that helpers are working (after including them into the ActionMailer class)

I am using: Rails 4.0.0 on Ruby 2.0.0-p247

Error Message:

ActionView::MissingTemplate at /events/81/user_invitations
==========================================================

> Missing partial user_invitation_mailer/test_partial with {:locale=>[:en], :formats=>[:text], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}. Searched in:
  * "/Users/user/Sites/participate/app/views"
  * "/Users/user/.rvm/gems/ruby-2.0.0-p247/gems/devise-3.4.0/app/views"

But this partial is there! - I am not allowed to upload pictures... structure is:

- views
-- user_invitation_mailer
--- _test_partial.html.erb
--- user_invitation.text.erb

Details:

call in the Controller:

UserInvitationMailer.user_invitation(@invitation).deliver

UserInvitationMailer:

class UserInvitationMailer < ActionMailer::Base
  add_template_helper(MailerHelper) # for testing including helpers.

  def user_invitation(invitation)
    @invitation = invitation
    mail(to: @invitation.email, subject: 'Invitation')
  end
end

user_invitation.text.erb:

Hi <%= @invitation.email %>,

This is a test email see:

<%# test_helper %>
<%= render partial: 'test_partial' %>

Thanks

_test_partial.html.erb:

"Test the partial."

(adding:) Not working:

<%= render partial: 'test_partial' %>
<%= render partial: './test_partial' %>
<%= render partial: '../test_partial' %>
<%= render partial: 'user_invitation_mailer/test_partial' %>

Upvotes: 4

Views: 7213

Answers (3)

FastIndian
FastIndian

Reputation: 242

The rails console give you nice explanation where it is looking for partials:

Missing partial user_invitation_mailer/test_partial with {:locale=>[:en], :formats=>[:text], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}.

Searched in:

  • "/Users/user/Sites/participate/app/views"

So the root folder for partials is your views folder. You can create new folder:

/views/partials/_some_partial.html.erb

and in your template:

render "partials/some_partials", local_variables: @model.some_variables

or just render full path partial:

render "/user_invitation_mailer/test_partial"

UPD

For complete answer please see comments below

Upvotes: 8

pra
pra

Reputation: 363

Were you able to find a solution? I am having the same issue. But able to render a partial if defined some shared folder. Edit - Found the issue. I was not properly closing the call.

Upvotes: 0

Mani David
Mani David

Reputation: 1412

in rails 4 you can try with <%= render 'test_partial' %> it will work.

Upvotes: 0

Related Questions