我有一篇文章,其中有很多評論。我正在嘗試向帖子的所有者發送通知電子郵件,表示有人對他們的帖子發表了評論,但我很難嘗試將帖子的所有者轉到郵件程序。的代碼,如果如下將用戶信息傳遞給郵件程序
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
那到底錯在哪裏? –
我想獲得郵寄用戶,以便我可以通過郵件將郵件通知他們評論 – erickeno
這一切都取決於您的模型的實現。請顯示可評論的內容,以及與之相關的模型。 – BroiSatse