订阅用户电子邮件与Ruby on Rails 5

问题描述:

我试图通过电子邮件创建订阅,订户应收到一封自动电子邮件,每当我在博客中创建新文章并订阅时。当我尝试这个功能时,我总是得到这个错误“表单中的第一个参数不能包含零或为空”。有什么建议么?订阅用户电子邮件与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

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

我认为错误在用户表单中: – BoB