Undefined method Class `all' in Rails 4 app - ruby-on-rails

I have been following various Ruby on rails REST API tutorials, specifically those using the GRAPE gem. On all these attempts, I have been encountering a undefined method `all' error. I am new to RoR and have been relying on the generators for model generation. Here is the error and code in question:
undefined method `all' for API::V1::EmailAuth:Class
module API
module V1
class EmailAuth < Grape::API
version 'v1' # path-based versioning by default
format :json # We don't like xml anymore
resource :email_auth do
desc "Return list of email_auth"
get do
EmailAuth.all # obviously you never want to call #all here
end
end
end
end
end
The file structure can be seen here: http://imgur.com/A2VnevL
(My rep is too low to post images)
This is my first time actually posting something in stackoverflow, do let me know if I can refine the question.
Just a note: I have refered to Undefined method `all' for ActiveSupport in Rails 4.0.0 app and various similar questions regarding namespace issues, but can seem to resolve this issue.
Thanks!

If you have a class Grape::API::EmailAuth and in this class you want to load the active record class EmailAuth, then you need to reference it by two colons:
get do
::EmailAuth.all
end

Related

How to find where is 'uninitialized constant' called in 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

NoMethodError (undefined method `encode' for JsonWebToken:Module):

I am referring to this blog for token based authentication. I am trying to access the jsonwebtoken.rb methods as specified in the above blog in my app/controllers/api/v1/registrations_controller.rb.
class Api::V1::RegistrationsController < Api::V1::BaseController
def create
auth_token = JsonWebToken.encode({user_id: user.id})
end
end
end
My jsonwebtoken.rb file is inside app/lib folder as specified in the blog mentioned above. I have no idea how to use the methods of jsonwebtoken.rb in my different controllers.
Also I have added config.autoload_paths << Rails.root.join('lib') to autoload the file in config/application.rb. Please help me. Somewhere, I am missing the concept.
I think the issue is in the file naming convention see your file name is jsonwebtoken.rb but the class is JsonWebToken so the file name will json_web_token.rb look at the console
2.3.4 :019 > "JsonWebToken".underscore
=> "json_web_token"
Rails UnderScore method.
Restart the server after made any changes.
Here is the very nice tutorial for RESTful JSON API With Rails 5, you can check this.

Safe and best way to monkey patch a rails gem

I have tried, seriously. Many questions out there but many developers say "It dont work for me"; I'm one of them -- said to say.
I was reading up on the best way to monkey-patch a rails gem. I've found few but decided to use this method.
I want to monkey-patch the xeroizer gem but rather the invoice.rb model.
# lib/xeroizer/invoice/invoice_url.rb
module Xeroizer
module Invoice
module InvoiceUrl
def invoice_url(id)
#application.http_get(#application.client, "#{url}/#{CGI.escape(id)}/OnlineInvoice")
end
end
end
end
Going with the "this method" link, I assume this should work, but it dosent.
Controller:
include Xeroizer::Invoice::InvoiceUrl
# Invoice.include Xeroizer::Invoice::InvoiceUrl
def some_method
# #xero is in a private method. It's here for short demonstration
#xero = Xeroizer::PrivateApplication.new("MY_CONSUMER_KEY", "MY_SECRET_KEY", "#{Rails.root}/privatekey.pem")
Rails.logger = #xero.Invoice.invoice_url("ad61ea97-b9e9-4a1e-b754-7c19e62f8cd7")
end
undefined method `invoice_url' for Xeroizer::Record::InvoiceModel
How do you add custom methods to a rails gem's class?
Assuming you are trying to monkey-patch Xeroizer::Record::InvoiceModel with Xeroizer::Invoice::InvoiceUrl, you might just do the following right after the first mention of Xeroizer::Record::InvoiceModel (to make Rails to autoload it):
Xeroizer::Record::InvoiceModel.prepend Xeroizer::Invoice::InvoiceUrl
This will override original invoice_url method. The original one still might be called from a prepended using super.

Rails 3: Access polymorphic_url method in separate module

I know I can call url_helpers in a separate module lib/utils.rb like this
module Utils
def self.get_url(object=nil)
if object.nil?
return Rails.application.routes.url_helpers.root_url
else
return Rails.application.routes.url_helpers.polymorphic_url(object)
end
end
end
How can I access polymorphic_url? With this code, I get:
undefined method `polymorphic_url' for #<Module:0x00000004be0128>
I'm using Rails 3.2.
Thanks
You should consult with APIdocks on such matters. Depending on Rails version, you can find polymorphic_url in a couple of different places:
Rails 4.
Rails 3.
For Rails 4, all you need to do is to include appropriate module to your module/class:
include ActionController::PolymorphicRoutes
That's it!

Rails3: warning: toplevel constant ApplicationController referenced by

Everytime i get a warning:
app/controllers/agency/agencies_controller.rb:1: warning: toplevel constant ApplicationController referenced by Agency::ApplicationController
My agencies_controller.rb:
class Agency::AgenciesController < Agency::ApplicationController
def index
...
end
...
end
And Agency::ApplicationController:
class Agency::ApplicationController < ApplicationController
layout 'agency'
helper_method :current_agency
private
def current_agency
#current_agency ||= current_user.agency
end
end
What the rails wants from me? What is the trouble?
Same situation with another controller
class Agency::ClientsController < Agency::ApplicationController
...
end
And no warnings, no errors...
I realize this question is almost two years old but I recently stumbled upon this through another stackoverflow post and wanted to share some insight.
Basically, if your namespace Agency happens to be a class instead of a module, you'll get that warning. In the stackoverflow post I pasted above, they had a model (class) of Admin and their namespace was also Admin.
This provides a better explanation of what is happening.
So check to see if your code isn't defining an Agency class somewhere. Good luck.
I had similar issues running Spork and Watchr in my Admin namespaced controllers. So i've fixed this by adding following code into each_run block in spec_helper.rb:
Dir[File.expand_path("app/controllers/admin/*.rb")].each do |file|
require file
end
All credits goes to guy from this thread
ApplicationController is the name of the superclass controller that Rails generates for you when you create a new project that all your other controller classes inherit from. There's probably a conflict somewhere because you've used the same name, even though you put it within a namespace.
Try giving your Agency::ApplicationController a different name.
I had similar issues, after setting up Spork and Watchr. In the process, I turned off class cacheing (config_cache_classes => false in config/environments/test.rb) so that changes would be reloaded as necessary in the spork environment. Turning class cacheing back on made the warnings go away.
In my case it was the problem with Devise. I had a devise model Admin and a namespaced routes Admin. Changing the namespaced route to Admins solved the problem.
Solution for me was add this line:
# spec/rails_helper.rb
Dir[File.expand_path("app/controllers/admin/*.rb")].each { |file| require file }

Resources