简体   繁体   中英

NoMethod Error private method ActionMailer rails 4 sending email after record save

I am new to rails and I'm making an app where a user can create a Post. I am trying to trigger a confirmation email after a user posts a Post....but I keep getting this NoMethod Error:

"private method `add_post' called for PostMailer:Class"

I've tried restarting and even not passing params. I have no idea what's going on. My ActionMailer logic is not encased in a 'private method'...as far as I know.

class PostsController < ApplicationController

  def add_post
    @magazine = Magazine.find(params[:m])
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        PostMailer.add_post(@post, @magazine, current_user).deliver
        format.html { redirect_to postadded_path(), notice: "Post Added."}
        format.json { render :show, status: :created, location: @post }
      else
        render :new
      end
    end
  end

The ActionMailer Logic

class PostMailer < ActionMailer::Base
  def add_post(post, magazine, current_user)
    @magazine = magazine
    @post = post
    @recipient = current_user.email
    mail(to: @recipient, subject: 'You've Posted an Article',   sent_on: Time.now,   template_path: 'add_post_email') 

  end

So...this was totally my problem. I left and "end" below "class PostMailer < ActionMailer::Base " like this. So my logic was right, but my error was ending the class before the logic could be read. Simple error, hours of headache.

class PostMailer < ActionMailer::Base
end 

def add_post(post, magazine, current_user)
    @magazine = magazine
    @post = post
    @recipient = current_user.email
    mail(to: @recipient, subject: 'You've Posted an Article',   sent_on: Time.now,   template_path: 'add_post_email') 
  end

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