Rails 4将邮件传递给邮件程序

时间:2015-07-07 19:00:51

标签: ruby-on-rails actionmailer

我尝试向发帖时订阅其他用户的用户发送电子邮件。

我收到错误:

ActiveRecord::RecordNotFound - Couldn't find User with 'id'=272:

272是其中一个订阅的ID。我在这里对所有活动部件感到困惑。如何提取订阅者电子邮件?

帖子控制器

def create
    @post = current_user.posts.new(post_params)
    if @post.save
        @subscribers = current_user.subscribers
        @subscribers.each do |f|
            SubscriptionMailer.send_prayer_request(@post, f).deliver
        end
 [...]

订阅邮件程序

class SubscriptionMailer < ActionMailer::Base
   def send_prayer_request(post, subscriber)

    @u = User.find(subscriber)
    mail(   :to => @u.email,
                :from => "services@coprayers.com",
                :subject => "New Post! "
            )
    end

end

用户模型

def subscribers
   Subscription.where(subscribe_to_id: self.id)
end

相关表格的架构

create_table "subscriptions", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "subscribe_to_id"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
end

create_table "users", force: :cascade do |t|
    t.string   "email",         default: "", null: false
    t.string   "username",      default: "", null: false
    t.string   "first_name"
    t.string   "last_name"
end

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

@u = User.find(subscriber)更改为@u = User.find(subscriber.user_id)否则,您正在尝试查找ID与订阅者ID相匹配的用户(与订阅者的user_id不同)。