Search code examples
ruby-on-railsnomethoderroractioncontroller

NoMethodError in controller that has methods defined


I finished implementing and testing a controller, then switched back and forth between Git branches and did some merges here and there, etc. Now I'm unable to use the methods I've defined for the controller, and also very confused, as I'm getting NoMethodError when trying to call them.

Added an edit and solution at the end of the post.


Using Rails version 5.2.3 - Here I've got my controller defined: app/controllers/paypal_access_token_controller.rb:

class PaypalAccessTokenController < ApplicationController
  before_action :authenticate_admin!, only: [:show]

  def njurf
    puts "#######################"
    puts "why can't i call these methods dangit"
    puts "#######################"
  end

  def show
    # doesn't really matter
  end

  def update
    # doesn't really matter either
    # no syntax errors, I promise
  end
end

I'd like the update method to be called when I start the Rails application. I added PaypalAccessToken.update() to the file config/environments/development.rb - this worked. Now that I've implemented stuff in other seemingly unrelated parts of the application, I can't call on methods from this controller anymore.

I removed the line from config/environments/development.rb so that I could run the Rails console and added the njurf method to the controller. From the console, I tried calling PaypalAccessTokenController.njurf, and PaypalAccessTokenController.update, but both give me the bespoke NoMethodError.

Here's some proof of concept

Loading development environment (Rails 5.2.3)
irb(main):001:0> PaypalAccessTokenController.update
Traceback (most recent call last):
        1: from (irb):1
NoMethodError (undefined method `update' for PaypalAccessTokenController:Class)
irb(main):002:0> PaypalAccessTokenController.update()
Traceback (most recent call last):
        1: from (irb):2
NoMethodError (undefined method `update' for PaypalAccessTokenController:Class)
irb(main):003:0> PaypalAccessTokenController.njurf()
Traceback (most recent call last):
        1: from (irb):3
NoMethodError (undefined method `njurf' for PaypalAccessTokenController:Class)

So the controller class exists, at least, but I don't know why I'm getting this error - nor do I know how to go about fixing it.

Any help would be appreciated.

edit: This controller only had routes for the show method. I removed this route when I had implemented and tested the update method, because I no longer needed/wanted these methods to be accessible via URLs. This is what made the methods inaccessible from console.


solution: Either I leave in a route to one of the controller's methods - or, like Ricky Spanish and amit_saxena pointed out below, properly declare the methods as class methods.

Thanks for the replies


Solution

  • You can call it, when you define it with self

    def self.njurf
      puts "#######################"
      puts "why can't i call these methods dangit"
      puts "#######################"
    end
    
    PaypalAccessTokenController.njurf
    #######################
    why can't i call these methods dangit
    #######################
    => nil