Search code examples
ruby-on-railsrubyrailstutorial.org

Hartl's rails tutorial ch11: Why is my account_activation mailer template preview not working?


I am on ch11 of Hartl's rails tutorial, section 11.2.1 Mailer Templates, The preview for the password reset mailer works fine but the account_activation preview does not. the error actually changed even though i didn't make any changes to the code, which was confusing. However the error that I am currently receiving is

NoMethodError in Rails::Mailers#preview

Showing /home/ubuntu/workspace/sample_app/app/views/user_mailer/account_activation.html.erb where line #3 raised:

undefined method `name' for nil:NilClass

Extracted source (around line #3)

<h1>Sample App</h1>

<p>Hi <%[email protected] %>,</p>

<p>
Welcome to the Sample App! Click on the link below to activate your account:

here is my user_mailer.rb

class UserMailer < ApplicationMailer

  def account_activation(user)
    @user = user
    mail to: user.email, subject: "Account activation"
  end

  def account_activation
    @greeting = "Hi"

    mail to: "[email protected]"
  end

  def password_reset
    @greeting = "Hi"

    mail to: "[email protected]"
  end
end

account_activations_controller.rb (no code has been specified so far in the tutorial)

class AccountActivationsController < ApplicationController
end

and the source of the error account_activation.html.erb

<h1>Sample App</h1>

<p>Hi <%[email protected] %>,</p>

<p>
Welcome to the Sample App! Click on the link below to activate your account:
</p>

<%= link_to "Activate", edit_account_activation_url(@user.activation_token,
                                                    email: @user.email) %>

it seems to not be recognising the name of the user. why could this be? (Note: all code is copied from the tutorial so i am at a loss as to the reason for the error) thanks.

EDIT:As per the comment there wer indeed 2 account_activation methods. i have deleted the second one and kept the first. Now the error has changed to an argument error.

ArgumentError in Rails::MailersController#preview 
wrong number of arguments (given 0, expected 1)

  def account_activation(user)
    @user = user
    mail to: user.email, subject: "Account activation"
  end

This method seems to be used in the following file without an argument.

/sample_app/test/mailers/previews/user_mailer_preview.rb

class UserMailerPreview < ActionMailer::Preview

  # Preview this email at http://localhost:3000/rails/mailers/user_mailer/account_activation
  def account_activation
    user = User.first
    user.activation_token = User.new_token
    UserMailer.account_activation(user)
    UserMailer.account_activation
  end

  # Preview this email at http://localhost:3000/rails/mailers/user_mailer/password_reset
  def password_reset
    UserMailer.password_reset
  end

end

Solution

  • In your UserMailer you currently have 2 versions of the account_activation method - one that takes a user parameter and a second one that doesn't. In Ruby you can't have 2 methods in a class with the same name so that second method will be replacing the earlier one.

    I'm not familiar with with all the contents of this tutorial, but it seems likely the method without the parameter is from earlier in the tutorial and should have been removed now.

    Then you will need to check the code where you're calling UserMailer.account_activation.deliver... and update it to be passing a user.


    Update

    In user_mailer_preview.rb you are calling account_activation twice:

    ...
    UserMailer.account_activation(user)
    UserMailer.account_activation
    ...
    

    The call without user should be removed.