Ruby on rails 将用户信息传递给邮件发送者
我有一个帖子有很多评论。我试图发送一封通知邮件给帖子的所有者,有人对他们的帖子发表了评论,但我很难让帖子的所有者回复邮件。代码如下所示Ruby on rails 将用户信息传递给邮件发送者,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我有一个帖子有很多评论。我试图发送一封通知邮件给帖子的所有者,有人对他们的帖子发表了评论,但我很难让帖子的所有者回复邮件。代码如下所示 class CommentsController < ApplicationController def create @commentable = find_commentable @comment = @commentable.comments.build(comment_params) @comment.user_id =
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 CommentsController
下面是邮件代码
class PostMailer < ActionMailer::Base
default from: "contact@example.com"
def comment_posted(user)
@user = user
mail to: user.first_name, subject: "You have a new Comment!"
end
end
class PostMailer
下面是注释模型
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :commentable, :polymorphic => true
has_ancestry
end
class注释true
祖籍
结束
以及后模式
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments, :as => :commentable, dependent: :destroy
end
class Post:可注释,依赖::destroy
结束
我注意到您在控制器中执行以下操作:
PostMailer.comment_posted
这不应该是:
PostMailer.offer_posted(@comment.user).deliver
?使用以下方法:
PostMailer.comment_posted(@commentable.user).deliver
@commentable
将为给定注释提供相应的Post
记录Post属于用户,因此您可以使用@commentable.user
访问海报。那么到底出了什么问题?我想获得Post用户,以便我可以通过邮件发送者发送电子邮件通知他们评论。这一切取决于您的模特的真实情况。请说明什么是可评论的,以及它与什么模型相关。我刚刚编辑了上面的问题并添加了模型PostMailer方法为什么称为offer\u posted?您正在调用CommentsController中发布的PostMailer.comment\u。这是一个输入错误,但在我的代码中它是正确的,我刚刚修复了它