简体   繁体   中英

How to pass params into a mailer method? Rails 6

I'm using Rails 6 and ActionMailer . I'm working on sending a mail to the user everytime his post is commented on. Here's my code:

app/mailers/comment_mailer.rb

class CommentMailer < ApplicationMailer
  def comment_mail
    @user = params[:user]

    mail(to: @user.email, subject: "Comments")
  end
end

app/controllers/comments_controller.rb

class CommentsController < ApplicationController
  before_action :authenticate_user!

  def create
    comment = Comment.new(comment_params)
    comment.user_id = current_user.id
    if comment.save
      CommentMailer.with(user: @user).comment_mail.deliver_now
      redirect_to post_path(comment.post.id)
    end
  end

app/views/comment_mailer/comment_mail.haml

%h4
  Hi
  - @user.name
%p Someone commented your post! Click the link below to see it:
  (I haven't done the link step yet)

After following this tutorial: https://dev.to/morinoko/sending-emails-in-rails-with-action-mailer-and-gmail-35g4 I had this Error:

NoMethodError in CommentsController#create
undefined method `email' for nil:NilClass

My console:

NoMethodError (undefined method `email' for nil:NilClass):


app/mailers/comment_mailer.rb:5:in `comment_mail'
app/controllers/comments_controller.rb:8:in `create'
Started POST "/comments" for 172.17.0.1 at 2020-07-01 00:32:38 +0000
Cannot render console from 172.17.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by CommentsController#create as HTML
  Parameters: {"authenticity_token"=>"Mf15U/OG9DX3SKwoBRHE/D/xENBocTtdcS07aUur+p/tGJAWSxYSP65kovzhLXHXBjvs/Wzp2dV4/1+L4nxrdQ==", "comment"=>{"content"=>"well said!", "post_id"=>"2"}, "commit"=>"Comment"}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
   (0.3ms)  BEGIN
  ↳ app/controllers/comments_controller.rb:7:in `create'
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/comments_controller.rb:7:in `create'
  Post Load (0.4ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/comments_controller.rb:7:in `create'
  Comment Create (0.7ms)  INSERT INTO "comments" ("content", "user_id", "post_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["content", "well said!"], ["user_id", 1], ["post_id", 2], ["created_at", "2020-07-01 00:32:38.439626"], ["updated_at", "2020-07-01 00:32:38.439626"]]
  ↳ app/controllers/comments_controller.rb:7:in `create'
   (1.1ms)  COMMIT
  ↳ app/controllers/comments_controller.rb:7:in `create'
CommentMailer#comment_email: processed outbound mail in 1.4ms
Completed 500 Internal Server Error in 54ms (ActiveRecord: 3.6ms | Allocations: 29044)

I used binding.pry to review the comment_mail method and I had @user >> nil as return.

When you call CommentMailer.with(user: @user).comment_mail.deliver_now You haven't actually defined @user you mentioned that you have a user attached to the post record so use that

comment.post.user so now you have

CommentMailer.with(user: comment.post.user).comment_mail.deliver_now

Or you make user a parameter of comment_mail

class CommentMailer < ApplicationMailer
  def comment_mail(user)
    @user = user #and this makes @user available in your templates

    mail(to: @user.email, subject: "Comments")
  end
end

And as you would expect call it like so

CommentMailer.comment_mail(comment.post.user).deliver_now #or preferably deliver_later

Assuming that comment belong_to posts, or whatever is being commented on, and then also assuming posts belong_to a user. You need to populate that @user variable. Something like:

@user = comment.post.user

To get the user of the post being commented on.

I think this should then work

CommentMailer.comment_mail(@user).deliver_now

and then update the method itself

def comment_mail(user)
  @user = user

  mail(to: @user.email, subject: "Comments")
end

You should also be able to then use the @user object in the mail view template too if you want to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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