Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `after_create' for Controller:Class

I am unsure why i get the below error. I am new to rails so Any help and explanation will be much appreciated.

  1. i am trying to send emails to subscribers each time a blog is created.
  2. in my blog controller i have added after_create :send_email_to_subscribers
  3. in my blog controller i have also added the method send_email_to_subscribers
  4. i have put in place successfully ActionMailer
  5. this was initially working and suddenly stopped
  6. has the after_create method been decaprecated?
  7. the error prompts up when i click <%= link_to 'create new blog', new_usera_blog_path(current_usera) %>
  8. i tried using after_filter :send_email_to_subscribers instead of after_create :send_email_to_subscribers in my blogs controller but that prompted up more errors in the mailer_subscribe.rb file

The below is the current error i get :

undefined method `after_create' for BlogsController:Class

terminal shows error:

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'

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

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
like image 358
ARTLoe Avatar asked Sep 19 '26 04:09

ARTLoe


1 Answers

after_create is a model callback. Not a controller callback.

If you want to add a callback which is run after your create method you would do:

after_action :send_email_to_subscribers, only: [:create]

But, this still occurs before the response is sent and will slow down your response times! You should consider using a background process, there are several gems like Sidekiq and Rescue for this purpose.

like image 189
max Avatar answered Sep 21 '26 18:09

max



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!