2014-04-03 28 views
0

我有一篇文章,其中有很多評論。我正在嘗試向帖子的所有者發送通知電子郵件,表示有人對他們的帖子發表了評論,但我很難嘗試將帖子的所有者轉到郵件程序。的代碼,如果如下將用戶信息傳遞給郵件程序

class CommentsController < ApplicationController 
def create 

    @commentable = find_commentable 
    @comment = @commentable.comments.build(comment_params) 
    @comment.user_id = current_user.id 

    if @comment.save 
     flash[:notice] = "Successfully posted an offer." 
     PostMailer.comment_posted(----).deliver #this is the mail code 
     redirect_to @commentable 
    else 
     flash[:error] = "Error adding an offer." 
    end 
    end 
end 
下面

是郵件代碼

class PostMailer < ActionMailer::Base 
    default from: "[email protected]" 

    def comment_posted(user) 
    @user = user 

    mail to: user.first_name, subject: "You have a new Comment!" 
    end 
end 
下面

是註釋模型

class Comment < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :commentable, :polymorphic => true 
    has_ancestry 
end 

和Post模型

class Post < ActiveRecord::Base 

    belongs_to :user 
    has_many :comments, :as => :commentable, dependent: :destroy 
end 
+0

那到底錯在哪裏? –

+0

我想獲得郵寄用戶,以便我可以通過郵件將郵件通知他們評論 – erickeno

+0

這一切都取決於您的模型的實現。請顯示可評論的內容,以及與之相關的模型。 – BroiSatse

回答

1

使用此:

PostMailer.comment_posted(@commentable.user).deliver 

@commentable會給你相應的Post紀錄爲給予的註釋。 發佈所屬的用戶,因此您可以使用@commentable.user訪問海報。

+0

做了魔術,謝謝 – erickeno

+0

很高興幫助。 :) –

0

我注意到您正在執行的控制器:

PostMailer.comment_posted 

不應該是這樣:

PostMailer.offer_posted(@comment.user).deliver 

+0

這是一個錯字在這個問題,但在我的代碼它的正確,我只是修復它 – erickeno