the preview isn't working and keep getting different errors.

My schema.rb looks like this

ActiveRecord::Schema[7.0].define(version: 2022_12_31_110010) do
  create_table "users", force: :cascade do |t|
    t.string "emailadress"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

My user model looks like this:

def send_daily
    users = User.all(params[:id])
    users.each. do |user|
      UserMailer.send_daily_newsletter(user).deliver_now
    end
    redirect_to root_path, notice: 'Newsletter envoyée.'
end

my User.Mailer looks like this:

    class UserMailer < ApplicationMailer
      def send_daily_newsletter(user)
      @user = user
      mail(to: @user.emailadress, subject: 'Votre fait historique quotidien')
     end
    end

and my mailer preview looks like this:

class UserMailerPreview < ActionMailer::Preview
  def send_daily_newsletter
    user = User.all # Ou tout autre utilisateur de votre choix pour la prévisualisation
    UserMailer.send_daily_newsletter(user)
  end
end

I keep getting the following error :

undefined method `emailadress' for #<ActiveRecord::Relation [#<User id: 14, emailadress: "[email protected]", created_at: "2023-07-02 10:32:46.657533000 +0000", updated_at: "2023-07-02 10:32:46.657533000 +0000">, #<User id: 15, emailadress: "[email protected]", created_at: "2023-07-02 11:43:17.121218000 +0000", updated_at: "2023-07-02 11:43:17.121218000 +0000">]>

Do you guys have any recommendation on what I should change to be able to visualise correctly the emails in the preview ?? thanks a lot !

2

There are 2 best solutions below

0
spickermann On

You get that error in your UserMailerPreview because you assign User.all to user which means you are passing an ActiveRecord::Relation and not a single record to your newsletter method. Which than fails in this line in your mailer: mail(to: @user.emailadress, ...

To fix this issue change

class UserMailerPreview < ActionMailer::Preview
  def send_daily_newsletter
    user = User.all # Ou tout autre utilisateur de votre choix pour la prévisualisation
    UserMailer.send_daily_newsletter(user)
  end
end

to

class UserMailerPreview < ActionMailer::Preview
  def send_daily_newsletter
    user = User.first # or `last` or `find(1)` or any other method returning a single user
    UserMailer.send_daily_newsletter(user)
  end
end

And another issue you will need to fix is method in your controller (?):

def send_daily
  users = User.all(params[:id])
  users.each. do |user|
    UserMailer.send_daily_newsletter(user).deliver_now
  end
  redirect_to root_path, notice: 'Newsletter envoyée.'
end

User.all(params[:id]) is raise an error because the all method doesn't accept arguments. It should just be User.all without the params[:id].

2
Syed Samiuzzaman On

you have the issue in the following line


class UserMailerPreview < ActionMailer::Preview
  def send_daily_newsletter
    user = User.all # Ou tout autre utilisateur de votre choix pour la prévisualisation
    UserMailer.send_daily_newsletter(user)
  end
end

  class UserMailer < ApplicationMailer
      def send_daily_newsletter(user)
      @user = user
      mail(to: @user.emailadress, subject: 'Votre fait historique quotidien')
     end
    end

in UserMailerPreview you have user = User.all which will give you all the users now if you use user.emailaddress it will raise error as user is the list of users and that list does not have emailaddress.

if you loop through it, then you will get the emailaddress.

it's better to use the plural as a variable name when you will expecting multiple results from the database.

try to use like following which may resolve your confusion users = User.all