我試圖通過電子郵件創建訂閱,訂戶應收到一封自動電子郵件,每當我在博客中創建新文章並訂閱時。當我嘗試這個功能時,我總是得到這個錯誤「表單中的第一個參數不能包含零或爲空」。有什麼建議麼?訂閱用戶電子郵件與Ruby on Rails 5
這是架構:
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "body"
t.string "image_url"
t.string "video_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "subscribers", force: :cascade do |t|
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
這是模型:
class Subscriber < ApplicationRecord
after_create :send_mail
def send_mail
SubscriptionMailer.welcome_message(self).deliver
end
end
這是郵寄者:
class SubscriptionMailer < ApplicationMailer
def send_email(email,article)
@article = article
mail(to: email,
subject: 'XXXXXXX')
end
end
這是控制器:
class SubscribersController < ApplicationController
def new
@subscriber = Subscriber.new
end
def create
@subscriber = Subscriber.new(params[:subscriber])
@subscriber.save
redirect_to root_path
end
end
SubscriptionMailer.welcome_message(個體經營).deliver在這兒,在SubscriptionMailer定義WELCOME_MESSAGE方法? –
和在Subscriber模型中,您傳遞一個參數,即SubscriptionMailer.welcome_message(self),並在郵件中傳遞兩個參數。 –
我想在send_mail方法裏面,實際上你是需要郵件的時候自己是整個訂閱者。你也傳遞一個參數而不是兩個,而且方法的名字是不同的。我寧願傳遞記錄的id,並在郵件中取代它,而不是整個對象。 – radubogdan