订阅者电子邮件与ruby on rails 5

时间:2017-04-06 08:30:22

标签: ruby-on-rails ruby rails-models

我试图通过电子邮件创建订阅,订阅者应该在我在博客中创建新文章和订阅时收到自动发送的电子邮件。我总是得到这个错误"表格中的第一个参数不能包含nil或为空#34;当我尝试这个功能。有什么建议吗?

这是架构:

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

1 个答案:

答案 0 :(得分:0)

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

在文章模型中

$(document).ready(function() {
  var txtFromDate, txtToDate;
  $("#txtFrom").datepicker({
    numberOfMonths: 1,
    onSelect: function(selected) {
      txtFromDate = selected;
      var dt = new Date(selected);
      dt.setDate(dt.getDate() + 1);
      $("#txtTo").datepicker("option", "minDate", dt);
    }
  });
  $("#txtTo").datepicker({
    numberOfMonths: 1,
    onSelect: function(selected) {
      txtToDate = selected;
      var dt = new Date(selected);
      dt.setDate(dt.getDate() - 1);
      $("#txtFrom").datepicker("option", "maxDate", dt);
    }
  });

  $('.submit-here').click(function() {
    // var link = day_1+month_1+year;
    var date1 = $("#txtFrom").datepicker('getDate'),
        day_1  = date1.getDate(),  
        month_1 = date1.getMonth() + 1,              
        year_1 =  date1.getFullYear();

    var date2 = $("#txtTo").datepicker('getDate'),
        day_2  = date2.getDate(),  
        month_2 = date2.getMonth() + 1,              
        year_2 =  date2.getFullYear(); 

    var where = $('#selection :selected').text();
    var people = $('#people :selected').text();

    console.log('www.lekkeslaap.co.za/akkommodasie-in/'+where+'?q='+where+'&start='+day_1+'+'+month_1+'+'+year_1+'&end='+day_2+'+'+month_2+'+'+year_2+'&pax='+people);   
  });
});