Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Calling a controller method from another controller -> NoMethodError

I am using the Devise gem and have the 2 controllers:

1: controllers/admin/mailings_controller.rb:

class Admin::MailingsController < ApplicationController
  def self.send_emails
    ...
    RegistrationsController.create
    ...
  end
end

2: controllers/registrations_controller.rb:

class RegistrationsController < Devise::RegistrationsController
  ...
  # POST
  def create
     # do some stuff
     super
     # do some stuff
  end
  ...
end

But when I execute (in the console)...

Admin::MailingsController.send_emails

...I get...

"NoMethodError: undefined method `create' for RegistrationsController:Class"

Why? And how to fix this?

rake routes

...shows no problem:

user_registration POST  ->  /users(.:format)  ->  registrations#create
like image 697
TomDogg Avatar asked Mar 16 '26 21:03

TomDogg


1 Answers

Change RegistrationsController.create to RegistrationsController.new.create

OR

change def create to def self.create.

The confusion you have here is using a class method where you mean to call an instance method. You can remedy this by either calling the method on an instance or changing the method to a class method.

like image 92
varatis Avatar answered Mar 18 '26 11:03

varatis



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!