How to find where is 'uninitialized constant' called in Rails? - ruby-on-rails

I'm on Rails 6 and using devise and cancancan (and lots of other gems too).
When I call User.find(id).destroy! in rails console, it rollbacks with
NameError Exception: uninitialized constant User::Interactive
Also tried with byebug with the same result.
My head is spinning when I think about looking through all the files for the place where it is called.
I'd appreciate a clue how to track an uninitialized constant with less costs.
UPDATE
Already done by looking through gems.
UPDATE 2 - controller
class UsersController < ApplicationController
load_and_authorize_resource
#REST actions
end

The issue is that you are trying to call this in IRB, which doesn't load your application.
Try running it in the rails console instead ($ rails c)

so you should have controller like this.
class User::InteractiveController < ApplicationController
end

Related

What is this access line of code in my Rails controller?

I have the following in my controller but I don't know what the access line means. I'm new to Rails 4. Is it for some gem like devise or something? I've looked through Google and don't see any documentation for the second line here. Any help would be great!
class MyCoolController < ApplicationController
access user: :all, all: [:my_action_name]
You can ask Ruby where the method was defined by booting your application / loading that controller with some debugging inserted:
class MyCoolController < ApplicationController
p method(:access).source_location

How to include respond_to when you don't inherit from ApplicationController in Rails 4?

I have an API controller in a Rails 4.1.2 app that does not inherit from Application controller. I'm trying to include the respond_to method and get a method undefined error....So then I required actionpack at the top like below:
require 'action_pack'
class Api::V1::UsersController < Api::ApiController
version 1
doorkeeper_for :all
respond_to :json
def show
respond_with current_user.as_json(except: :password_digest)
end
end
and I still get
ActionController::RoutingError (undefined method `respond_to' for Api::V1::UsersController:Class):
But the respond_to method is part of the MODULE ActionController::MimeResponds::ClassMethods which can be found under the action_pack folder in a subdirectory if I open up the actionpack gem source code.
EDIT:
I should also mention Api::ApiController has a parent RocketPants::Base since I'm using the rocketpants api gem ... The gem's author states on his README: "RocketPants only includes the bare minimum to make apis. In the near future, it may be modified to work with ActionController::Base for the purposes of better compatibility with other gems."
I'm particularly interested in how to get access to the action_pack methods/libraries (if it's possible) in a standalone fashion, the way you can when you include activesupport.
Simply make your Api::ApiController (or its parent if there is one) inherit from ActionController::Base
Good thing you mention you are using RocketPants! A quick look at their github page confirm that it is already derivated from ActionController::Base. That renders my previous answer incorrect.
So it seems you don't need to call respond_to and that instead of respond_with you should use expose, but well it's a blink guess. You should follow their documentation
Also check your rails version. In Rails 4.2 and up, the respond_to syntax has been moved into the
responders gem.
Gemfile:
gem 'responders'
Console:
bundle install
rails g responders:install
I saw this error ActionController::RoutingError: undefined method 'respond_to' while I was using rails-api gem. After switching ActionController::API to ActionController::Base my error was resolved. This also fixed active_model_serializers failing to wrap render :json automatically with the corresponding serializers to my models. I may log this as an issue with the gem.
You have to include (at least) the ActionController::RespondWith module.

Rails, custom exceptions

I'm following instructions at http://ariejan.net/2011/10/14/rails-3-customized-exception-handling/ and have hit a road block.
I am relatively new to rails, so I'm not sure what I've done right/not-so-right.
The first step was to create class
MyApp::ProfileNotFoundError < StandardError
end
So I went to app/models and created profile_not_found.rb which contains the following, where (APP) is the name of my app as defined by Rails.application.class.parent_name, but I have hidden from this post for security/privacy.
(APP)::ProfileNotFoundError < StandardError
end
In app/controllers/application_controller.rb I added
rescue_from (APP)::ProfileNotFoundError, :with => :profile_not_found
and in my login controller I added
raise (APP)::ProfileNotFoundError if #profile.nil?
However, when I try to test the code, I get a Routing Error stating
uninitialized constant (APP)::BlankUsernameError
In my opinion, this suggests that I did something wrong pertaining to the class creation, but the tutorial is so vague I can't figure it out. Any pointers?
I'm running Rails 3.0.20 & Ruby 1.8.7 on Ubuntu 12.04.2 x86_64
Do you have the class keyword in your class definition?
class MyApp::ProfileNotFoundError < StandardError
end
Secondly, you'll have to require your exceptions where you're using it. This is probably the problem you're encountering with the uninitialized constant error. To do this you will probably have to wrap it in a module:
module Exceptions
class MyApp::ProfileNotFoundError < StandardError
end
end
Also, you should put your error classes in a different directory than /models. This directory should be explicitly for your models. Maybe make one like /errors.

Rails: request.subdomains raises undef method for request when in application controller

I am trying to extract the subdomain in the application controller, for app wide use, this way,
#subdomain = request.subdomains(0)
While this code works in any other controller, in the app controller it throws an
undefined local variable or method `request' for ApplicationController:Class
exception.
I am running rails 3.2.2 on Lion.
I borrowed it from DHH's code fragment for basecamp style subdomains -
class ApplicationController < ActionController::Base
  before_filter :set_current_account
  private
    def set_current_account
      #current_account = Account.find_by_subdomain!(request.subdomains.first)
    end
end
What am I missing?
When you're getting that error, it's happenning becaue you're calling request inside ApplicationController's class instead of inside a method. Are you sure that the code you've shown us is correct?

Spork and aliased helper method in Rails application

I have problem with Spork (v 1.0.0rc1) and Devise in my Rails application. I have this error when I run spork:
undefined method 'user_signed_in?' for class ApplicationController
In ApplicationController I have following lines:
alias logged_in? user_signed_in?
helper_method :logged_in?
When I remove these two lines, Spork works fine. I use logged_in? for historical reasons in views and I would like to continue to do so.
I have looked around and I couldn't find anything helpful.
Go back to "~> 0.9.0.rc" until they resolve it.

Resources