简体   繁体   English

Rails actionmailer发送可评论模型的电子邮件

[英]Rails actionmailer sending emails for commentable model

I'm trying to send emails through my actionmailer to the commentable owners' email after another user writes them a comment but I keep getting an error. 我试图在其他用户向他们发送评论后,通过我的actionmailer将电子邮件发送到可评论的所有者的电子邮件,但我一直收到错误消息。 Can someone help me out with this? 有人可以帮我这个忙吗? Thanks in advance. 提前致谢。

comment_mailer.rb comment_mailer.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

comment.rb comment.rb

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

error 错误

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'

comments_controller comments_controller

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 

You error is in the send_email method. 您的错误在于send_email方法中。 There is no local variable called comment . 没有称为comment局部变量。 You are already inside an instance of comment, the instance you want to send an email about. 您已经在要发送电子邮件的评论实例中。 So you want to use the keyword self instead. 因此,您想改用关键字self

Change this: 更改此:

def send_email
  CommentMailer.email_notification(member, comment).deliver
end

To this: 对此:

def send_email
  CommentMailer.email_notification(member, self).deliver
end

self refers to the current instance of comment, which you want to use for your mailer. self是您要用于邮件程序的当前注释实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM