Ruby on rails Rails actionmailer为可评论模型发送电子邮件
我试图通过我的actionmailer在其他用户写评论后发送电子邮件到可评论所有者的电子邮件,但我不断收到错误。有人能帮我解决这个问题吗?提前谢谢 评论\u mailer.rbRuby on rails Rails actionmailer为可评论模型发送电子邮件,ruby-on-rails,email,actionmailer,acts-as-commentable,Ruby On Rails,Email,Actionmailer,Acts As Commentable,我试图通过我的actionmailer在其他用户写评论后发送电子邮件到可评论所有者的电子邮件,但我不断收到错误。有人能帮我解决这个问题吗?提前谢谢 评论\u mailer.rb def email_notification(member, comment) @member = commentable.member @sender = comment.member mail to: commentable.member.email, subject: "#{comment.
def email_notification(member, comment)
@member = commentable.member
@sender = comment.member
mail to: commentable.member.email, subject: "#{comment.member.full_name} (#{comment.member.user_name}) has left you a comment"
end
belongs_to :member
belongs_to :commentable, polymorphic: true
attr_accessible :content
after_create :send_email
def send_email
CommentMailer.email_notification(member, comment).deliver
end
comment.rb
def email_notification(member, comment)
@member = commentable.member
@sender = comment.member
mail to: commentable.member.email, subject: "#{comment.member.full_name} (#{comment.member.user_name}) has left you a comment"
end
belongs_to :member
belongs_to :commentable, polymorphic: true
attr_accessible :content
after_create :send_email
def send_email
CommentMailer.email_notification(member, comment).deliver
end
错误
undefined local variable or method `comment' for #<Comment:0x51c2ad8>
app/models/comment.rb:18:in `send_email'
app/controllers/comments_controller.rb:20:in `block in create'
app/controllers/comments_controller.rb:19:in `create'
before_filter :authenticate_member!
before_filter :load_commentable
before_filter :find_member
def index
redirect_to root_path
end
def new
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(params[:comment])
@comments = @commentable.comments.order('created_at desc').page(params[:page]).per_page(15)
@comment.member = current_member
respond_to do |format|
if @comment.save
format.html { redirect_to :back }
format.json
format.js
else
format.html { redirect_to :back }
format.json
format.js
end
end
end
def destroy
@comment = Comment.find(params[:id])
respond_to do |format|
if @comment.member == current_member || @commentable.member == current_member
@comment.destroy
format.html { redirect_to :back }
format.json
format.js
else
format.html { redirect_to :back, alert: 'You can\'t delete this comment.' }
format.json
format.js
end
end
end
private
def load_commentable
klass = [Status, Medium, Project, Event, Listing].detect { |c| params["#{c.name.underscore}_id"] }
@commentable = klass.find(params["#{klass.name.underscore}_id"])
end
def find_member
@member = Member.find_by_user_name(params[:user_name])
end
您的错误出现在
send\u email
方法中。没有名为comment
的局部变量。您已经在comment实例中,该实例是您要发送电子邮件的对象。因此,您希望使用关键字self
更改此项:
def send_email
CommentMailer.email_notification(member, comment).deliver
end
为此:
def send_email
CommentMailer.email_notification(member, self).deliver
end
self
引用当前的comment实例,您希望将其用于邮件发送程序