ActionMailer starts Preview and gets in trouble if the argument is wrong ( wrong number of arguments (given0, expected1)), so I want to resolve it.

Asked 2 years ago, Updated 2 years ago, 465 views

I would like to use HerokuScheduler to run the rake task at the specified time, but there is an error in the preview (test/mailers/previews/alert_mailer_preview.rb) of the mail sending function that I want to run.

Error Description: wrong number of arguments (given0, expected1)

The user should have been passed as an argument, but on the Preview screen, it is still in a state of confusion.I would appreciate your help.

Enter a description of the image here

If you click send_alert after this, you will be scolded for saying "wrong number of arguments (given0, expected1)" as shown below.

Enter a description of the image here

Ruby: 2.6.5
Rails:5.2.4
DB: PostgreSQL
Heroku Scheduler

mailers/previews/alert.mailer_previrew.rb

#Preview all emails at http://localhost:3000/rails/mailers/alert_mailer
class AlertMailerPreview <ActionMailer::Preview
  def send_alert(user)
    @user=user
    mail(
      to :@user.email,
      subject: 'Register your practice record!'
    )
    # mail to: "#{@user.email}", subject: "Hello, #{@user.email}"
    # mail to:user.email,subject: 'Register your practice records!'
  end
end
app/mailers/application_mailer.rb

class ApplicationMailer<ActionMailer::Base
  default from: '[email protected]'
  layout'mailer'
end

terminal

Started GET"/rails/mailers/alert_mailer/send_alert" for::1 at 2020-06-30 15:53:55+0900
Processing by Rails::MailersController #preview as HTML
  Parameters: {"path"=>"alert_mailer/send_alert"}
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)

ArgumentError - wrong number of arguments (given0, expected1):
  test/mailers/previews/alert_mailer_preview.rb:3:in`send_alert'

Started POST "/__better_errors/30c7823ed90f92ce/variables" for::1 at 2020-06-30 15:53:55+0900

app/views/alert_mailer/send_alert.html.slim
app/views/alert_mailer/send_alert.text.html.slim

Dear
=user.mail,

Thank you for using TTManager.
br/
Have you registered your practice record?If you are not done, please log in here.
br/
= link_to'https://vast-anchorage-69571.herokuapp.com/', do
  | Log in here
  span

app/models/user.rb

class User<ApplicationRecord
  # Include default device modules.Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  device:database_authenticatable,:registerable,:recoverable,:rememberable,:validatable, password_length:6..128
  has_many —Records

  def self.guest
    find_or_create_by (email: "[email protected]") do | user |
      user.password=SecureRandom.urlsafe_base64
          # Required if using user.confirmed_at=Time.now#Confirmable
    end
  end
end

app/mailers/alert.mailer.rb

class AlertMailer<ApplicationMailer
  def send_alert(user)
    @user=user
    mail to:@user.email,subject: 'Register your practice records!'
    # mail to: "#{@user.email}", subject: "Hello, #{@user.email}"
    # mail to:user.email,subject: 'Register your practice records!'
  end
end

ruby-on-rails ruby gmail

2022-09-30 21:50

1 Answers

Changing mailers/previews/alert.mailer_previrew.rb to the following eliminates the error:There is another error (No Method Error), but I will try to figure out a solution myself (Nakanishi, thank you for pointing it out).

Pre-correction code

class AlertMailerPreview<ActionMailer::Preview
  def send_alert(user)
    @user=user
    mail(
      to :@user.email,
      subject: 'Register your practice record!'
    )
    # mail to: "#{@user.email}", subject: "Hello, #{@user.email}"
    # mail to:user.email,subject: 'Register your practice records!'
  end
end

Corrected Code

class AlertMailerPreview<ActionMailer::Preview
  def send_alert
    @user=User.all
    AlertMailer.send_alert(@user)
  end
end

What I did


2022-09-30 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.