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 !
You get that error in your
UserMailerPreviewbecause you assignUser.alltouserwhich means you are passing anActiveRecord::Relationand 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
to
And another issue you will need to fix is method in your controller (?):
User.all(params[:id])is raise an error because theallmethod doesn't accept arguments. It should just beUser.allwithout theparams[:id].