Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionMailer::Preview uses different execution of mailer for email info bar and email content #24460

Closed
barnett opened this issue Apr 8, 2016 · 5 comments

Comments

@barnett
Copy link

barnett commented Apr 8, 2016

Steps to reproduce

Create a mailer:

class ObjectMailer < ApplicationMailer
  def event_happened
    @object = Object.new
    mail to: 'test@gmail.com', subject: "#{@object} Happened!"
  end
end

Create a preview:

# Preview all emails at http://localhost:3000/rails/mailers/account_mailer
class ObjectMailerPreview < ActionMailer::Preview
  def event_happened
    ObjectMailer.event_happened
  end
end

And in apps/views/object_mailer/event_happened.html.erb:

<p>@object</p>

The object will differ.

Example Repo

  1. Start server
  2. Go to /rails/mailers/object_mailer/event_updated
  3. 💥

Expected behavior

I would expect the render for the email and email info bar use the same execution. Thus the Object#id in the subject and within the email would be the same.

Actual behavior

They render with different instances.

System configuration

Rails version: 5.0.0.beta1
Ruby version: 2.3.0

@vipulnsward
Copy link
Member

The email content is displayed in an iframe and fetched in a separate request(based on whether html or text is selected in dropdown).

cc @pixeltrix if we are interested to update this header info as well. It would need a full-page reload/or not using this iframe though.

@pixeltrix
Copy link
Contributor

Yeah, we'd need to redesign the page so that the whole page would be re-rendered at the same time.

@barnett your example is very contrived - do you have a real-world example where this has affected you?

@barnett
Copy link
Author

barnett commented Apr 25, 2016

@pixeltrix sure!

In a PreviewMailer I am using Array#sample to select an Account instance that's in a specific state. The issue is, the body of the mailer will mention one Account, when the subject will mention another (since I have dynamic content in those areas based on the Account).

code sample:

class AccountMailerPreview < ActionMailer::Preview
  def upcoming_surveys
    accounts = Account.active.with_upcoming_surveys
    AccountMailer.upcoming_surveys(accounts.sample.id)
  end
end

While I could hypothetically use Array#first instead of Array#sample, but I like making sure I handle any possible edge cases by randomly choosing one.

@stale stale bot added the stale label Mar 28, 2017
@stale
Copy link

stale bot commented Mar 28, 2017

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-1-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@stale stale bot closed this as completed Apr 4, 2017
@dmytro-spivakov
Copy link

Reproduced on Rails 7.0.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants