【问题标题】:NoMethod Error private method ActionMailer rails 4 sending email after record saveNoMethod 错误私有方法 ActionMailer rails 4 在记录保存后发送电子邮件
【发布时间】:2015-01-05 07:53:50
【问题描述】:

我是 Rails 新手,我正在制作一个用户可以在其中创建帖子的应用程序。我试图在用户发布帖子后触发确认电子邮件....但我不断收到此 NoMethod 错误:

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

我尝试重新启动,甚至不传递参数。我不知道是怎么回事。我的 ActionMailer 逻辑没有包含在“私有方法”中……据我所知。

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

ActionMailer 逻辑

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

【问题讨论】:

  • 这太奇怪了。下面的工作吗? PostMailer.send(:add_post, @post, @magazine, current_user).deliver
  • 谢谢你的回答,我刚刚回来把我有的和额外的“end”放在“class PostMailer

标签: ruby-on-rails ruby-on-rails-4 actionmailer nomethoderror


【解决方案1】:

所以...这完全是我的问题。我离开并在“class PostMailer

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

【讨论】:

    猜你喜欢
    • 2014-12-31
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多