Sylar
Sylar

Reputation: 12072

Undefined local variable or method `params' - In Mailer

Story:

It goes something like, when a user hits a 'like' button on user's post it sends the post owner a notification. The route for each posts look like /users/:user_id/posts/:id

Assumption: SendGrid is configured and working ok.

Generating the post notification:

rails g mailer PostNotification

In app/mailers/post_notification.rb:

class PostNotification < ActionMailer::Base

  default :from => '[email protected]'

  def send_notification_email(user)
    user = User.friendly.find( params[:user_id] ) # params only work in the views/controller. How to get it to work here??
    mail( :to => user.email, # should send to that particular post owner
    :subject => "You've got a post like" )
  end
end

app/views/post_notification/send_notification_email.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>You've got a new like from, <%= current_user.name %>!</h1>
     <p>Blaahhhhhh</p>
  </body>
</html>

app/controller/posts_controller:

class PostsController < ApplicationController

  def like
   [...] # other working code
   if p.save
    PostNotification.send_notification_email(@user).deliver
  end

end

The idea is to grab the user_id for that post so that the notification/email goes to that user, with a message, in the body, from the (current_user). The trouble Im having is in the post_notification.rb. How to grab the user_id for that 'post owner' so the email goes to them?

Upvotes: 0

Views: 1426

Answers (2)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34308

ActionMailer does not have access to the controller params, so you can't access params hash inside your mailer.

You can pass the params you need as arguments instead like you passed the user in the argument in your send_notification_email method. Use that user's id if you need:

def send_notification_email(user)
  user = User.friendly.find(user.id) # use the id of the passed user
  mail( :to => user.email,
        :subject => "You've got a post like" )
end

Apparently, you can even omit user = User.friendly.find(user.id) # use the id of the passed user line because you already have the user object which you can directly use in the mail method call:

def send_notification_email(user)
  mail(:to => user.email,
        :subject => "You've got a post like")
end

Both of these should work. And the bottom line is: ActionMailer does not have access to the controller params hash which is the cause of your error. :-)

Upvotes: 2

Pavan
Pavan

Reputation: 33542

You don't need this line user = User.friendly.find( params[:user_id] ) actually. You are passing @user as an argument here send_notification_email(@user), so it is accessed as user in the mailer method. The below should work.

class PostNotification < ActionMailer::Base

  default :from => '[email protected]'

  def send_notification_email(user)
    mail( :to => user.email, :subject => "You've got a post like" )
  end
end

Upvotes: 1

Related Questions