2017-04-06 112 views
0

我試圖通過電子郵件創建訂閱,訂戶應收到一封自動電子郵件,每當我在博客中創建新文章並訂閱時。當我嘗試這個功能時,我總是得到這個錯誤「表單中的第一個參數不能包含零或爲空」。有什麼建議麼?訂閱用戶電子郵件與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 
+0

SubscriptionMailer.welcome_message(個體經營).deliver在這兒,在SubscriptionMailer定義WELCOME_MESSAGE方法? –

+0

和在Subscriber模型中,您傳遞一個參數,即SubscriptionMailer.welcome_message(self),並在郵件中傳遞兩個參數。 –

+0

我想在send_mail方法裏面,實際上你是需要郵件的時候自己是整個訂閱者。你也傳遞一個參數而不是兩個,而且方法的名字是不同的。我寧願傳遞記錄的id,並在郵件中取代它,而不是整個對象。 – radubogdan

回答

0
class Subscriber < ApplicationRecord 
    belongs_to :article 

    after_create :send_mail 

    def send_mail 
    SubscriptionMailer.welcome_message(self).deliver 
    end 
end 

在郵件

class SubscriptionMailer < ApplicationMailer 
    def welcome_message(subscriber) 
    @article = Article.joins(:subscribers).where("subscribers.id= ?", self.id) 
    @email = subscriber.email 
    mail(to: @email, 
    subject: 'XXXXXXX') 
    end 
end 

在文章模型

class Article < ApplicationRecord 
    has_many :subscribers 
    end 
+0

謝謝你的回答,我試過你的解決方案,但它不起作用,我總是得到同樣的錯誤。 – BoB

+0

你可以粘貼文章控制器和表單嗎? –

+0

我認爲錯誤在用戶表單中: <%form_for(@subscriber)do | f | %> <%= f.text_field:email%> <%= f.submit%> <% end %> – BoB