Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Action Mailer uninitialized constant

I'm trying to use actionmailer to notify me when a new comment has been posted but I keep getting the error:

uninitialized constant CommentsController::CommentMailer

The comment is added to my database and can be viewed. I am also using devise and it's email functions are working fine.

My comment mailer:

class CommentMailer < ActionMailer::Base
  def newcomment(comment)
  mail(:to => "[email protected]", :subject => "New Comment")  
  end  
end

and my controller section:

def create
  @comment = Comment.new(params[:comment])
  @comment.user_id = current_user.id

respond_to do |format|
  if @comment.save
    CommentMailer.newcomment(@comment).deliver
    format.html { redirect_to @comment, notice: 'Comment was successfully created!' }
    format.json { render json: @comment, status: :created, location: @comment }
  else
    format.html { render action: "new" }
    format.json { render json: @comment.errors, status: :unprocessable_entity }
  end
 end
end
like image 738
Steve Avatar asked Feb 13 '12 22:02

Steve


2 Answers

This can also happen if you name your mailer file wrong. UserMailer.rb will break whereas user_mailer.rb is what is expected.

like image 93
Uri Avatar answered Oct 05 '22 04:10

Uri


OK my bad, I had to restart my rails application after I added the mailer. It is working fine now

like image 31
Steve Avatar answered Oct 05 '22 06:10

Steve