Controller的未定义方法`after_create':Class

ART*_*Loe 2 ruby ruby-on-rails ruby-on-rails-4

我不确定为什么我得到以下错误.我是rails的新手,所以任何帮助和解释都将非常感激.

  1. 我想在每次创建博客时向订阅者发送电子邮件.
  2. 在我的博客控制器中,我添加了 after_create :send_email_to_subscribers
  3. 在我的博客控制器中,我也添加了方法 send_email_to_subscribers
  4. 我已成功落实到位 ActionMailer
  5. 这是最初的工作,突然停止了
  6. after_create方法是否已经decaprecated?
  7. 当我点击<%= link_to'创建新博客'时,错误会提示,new_usera_blog_path(current_usera)%>
  8. 我尝试使用after_filter :send_email_to_subscribers而不是after_create :send_email_to_subscribers在我的博客控制器中,但这在mailer_subscribe.rb文件中引发了更多错误

以下是我得到的当前错误:

博客控制器的未定义方法`after_create':Class

终端显示错误:

Started GET "/useras/1/blogs/2" for 127.0.0.1 at 2015-06-20 20:14:16 +0100
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by BlogsController#show as HTML
  Parameters: {"usera_id"=>"1", "id"=>"2"}
  Blog Load (0.1ms)  SELECT  "blogs".* FROM "blogs"  WHERE "blogs"."id" = ? LIMIT 1  [["id", 2]]
  Usera Load (0.1ms)  SELECT  "useras".* FROM "useras"  WHERE "useras"."id" = ? LIMIT 1  [["id", 1]]
  Blog Load (0.2ms)  SELECT  "blogs".* FROM "blogs"  WHERE "blogs"."usera_id" = ? AND "blogs"."id" = ? LIMIT 1  [["usera_id", 1], ["id", 2]]
  Rendered blogs/show.html.erb within layouts/application (3.0ms)
  Subscriber Load (0.2ms)  SELECT "subscribers".* FROM "subscribers"

MailerSubscribe#subscription_email: processed outbound mail in 7.8ms
Completed 500 Internal Server Error in 891ms (Views: 798.5ms | ActiveRecord: 1.4ms)

NoMethodError - undefined method `title' for #<BlogsController:0x007fae0af874f0>:
  app/mailers/mailer_subscribe.rb:6:in `subscription_email'
  actionpack (4.1.10) lib/abstract_controller/base.rb:189:in `process_action'
  actionpack (4.1.10) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.1.10) lib/active_support/callbacks.rb:82:in `run_callbacks'
  actionpack (4.1.10) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.1.10) lib/abstract_controller/base.rb:136:in `process'
Run Code Online (Sandbox Code Playgroud)

blogs_controller.rb

class BlogsController < ApplicationController
  respond_to :html, :xml, :json
  before_action :set_blog, only: [:show, :edit, :update, :destroy]
  after_create :send_email_to_subscribers

  def index
...
  end

  def show
...
  end

  def new
    @usera = Usera.find(params[:usera_id])
    @blog = @usera.blogs.build
    respond_with(@blog)
  end

  def edit
  end

  def create
    @usera = Usera.find(params[:usera_id])
    @blog = @usera.blogs.create(blog_params)
    respond_to do |format|
      if @blog.save
        format.html { redirect_to([@blog.usera, @blog], notice: 'Blog was successfully created.') }
        format.json  { render json: @blog, status: :created, location: @blog }
      else
        format.html { render action: "new" }
        format.json  { render json: @blog.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
...
  end

  def destroy
...
  end


  private
    def set_blog
      @blog = Blog.find(params[:id])
    end

    def blog_params
      params.require(:blog).permit(:title, :content, :usera_id, :link, :image, :category_blog_id, :image)
    end

    def send_email_to_subscribers
      Subscriber.all.each do |subscriber|
        MailerSubscribe.subscription_email(subscriber.email,self)
      end
    end
end
Run Code Online (Sandbox Code Playgroud)

mailer_subscribe.rb

class MailerSubscribe < ActionMailer::Base
  default from: "info@recruitmentafrica"

  def subscription_email(email,blog)    
    @blog = blog
    mail(to: email, subject: @blog.title)
  end
end
Run Code Online (Sandbox Code Playgroud)

max*_*max 7

after_create是一个模型回调.不是控制器回调.

如果要添加在create方法之后运行的回调,您可以执行以下操作:

after_action :send_email_to_subscribers, only: [:create]
Run Code Online (Sandbox Code Playgroud)

但是,这仍然会在响应发送之前发生,并会减慢响应时间!您应该考虑使用后台进程,为此目的有一些像SidekiqRescue这样的宝石.