undefined method `new' for Test:Module - ruby-on-rails

Hello I have a problem of conflict of the namespace. I have a model: Test and controller TestsController. server displays an error
undefined method `new' for Test:Module
I read this question rails models
added to the model Test in module UserTest
module UserTest
class Test < ActiveRecord::Base
....
end
end
and added to the controller
class TestsController < ApplicationController
def new
#test = UserTest::Test.new
#test.questions.build
#title = "New test"
end
...
end
server shows an error: uninitialized constant TestsController::UserTest
after reading more I realized that probably need to add require or include a controller. Only I do not understand how to do it. please tell me.

Never rename a model to the same name of the project. You will get a message like this:
undefined method `new' for Example:Module
The project module priority precedes on the call.

The convention in Rails is to convert your Class name in file and your module name in directory. So if you put your UserTest::Test class in test.rb file in your app/model directory, the autoload failed to get your class. Because search on app/model/user_test/test.rb file.
So you can "force" the require in your Controller by adding a require in top of your file. The require if you put your class in your test.rb is : require 'test.rb'
To know how define your require is to think the LOAD_PATH of your application add app/model directory. So all inside can be add directly by requiring the directory name and file name.

Related

Can not call method of other class from static method

At first, i have a class which in app/workes/ like this:
class SendMailTask
include Resque::Plugins::Status
require 'mail'
def perform
...
end
And as a controller, i have class UsersController and a static method like bellow:
class UsersController < ApplicationController
def self.check
...
::SendMailTask.create(to: [] << #to_addresses, subject: #subject, body: #body)
end
When i call method UsersController.check() from other file, i received the error: "in `block in check': uninitialized constant SendMailTask (NameError)"
But from other controller, i can call SendMailTask normally:
class ErrorController < ApplicationController
def index
...
::SendMailTask.create(to: [] << #to_addresses, subject: #subject, body: #body)
end
I try to add this line:
config.autoload_paths += %W(#{config.root}/app/workers)
to application.rb and try to add
require './SendMailTask'
at the begin of file users_controller.rb but it does not work.
Please help me resolve this error. Thanks you
NameError means the your SendMailTask isn't loaded. so you will have to load that. so couple of things.
I noticed a typo workes, so please verify the file name is correct. By Convention, it should be located at app/workers/send_mail_task.rb. so kindly double-triple check the same.
About require './SendMailTask', this is wrong. Instead it would be send_mail_task as requires works on filenames & not class names.
if still get an error, then please post your $LOAD_PATH to see you are requiring the file relative to the defined $LOAD_PATH
Instead of require, I prefer to use require_dependency as it works with code-reloading etc. so if you have trouble with auto-loading, just stick that require_dependency on top of the file, this will hint rails to load the file BEFORE running the controller.

Uninitialized constant while trying to include helper module

The module that I am trying to include is located here:
test/unit/helpers/test_helpers.rb
Looks like:
module TestHelpers
end
I am trying to include it in:
test/unit/app/models/abc.rb
class Abc < ActiveSupport::TestCase
include TestHelpers
end
gives the following error:
Error executing test/unit/app/models/abc.rb uninitialized constant
Abc::TestHelpers
Any ideas why this might be happening?
To include a module into your class, you need to require that file.
require 'test_helpers'
Add this line at the top of your model class.

Uninitialized Constant in Rails Controller

I have the following in my controller:
class SurveysController < ApplicationController
def index
survey_provider = FluidSurveysProviders::SurveyProvider.new
contact_lists = survey_provider.get_lists()
#survey = Survey.new(contact_lists)
end
And I'm receiving this error:
NameError in SurveysController#index
uninitialized constant SurveysController::FluidSurveysProviders
Excuse my Rails noobiness, I'm sure I'm leaving out something important here. But it seems to me that I am trying to "initialize" the constant with this line:
survey_provider = FluidSurveysProviders::SurveyProvider.new
But that's the same line that's throwing an error because it's not initialized. Where should I be "initializing" the Provider?
Once you require fluid_surveys_providers (or similar) then do this:
include FluidSurveysProviders
Make sure SurveyProvider is wrapped with module FluidSurveysProviders. It may look like this
module FluidSurveysProviders
class SurveyProvider
...
end
end
if its an ActiveRecord object try this
class FluidSurveysProviders::SurveyProvider < ActiveRecord::Base
...
end
The SurveyProvider was not loaded correctly.
For a quick fix, move the class file into app directory, e.g. app/lib/survey_provider.rb. Then all code inside app will be auto-loaded by Rails.
Or make sure the path to class SurveyProvider is included in the autoload_path of Rails. In config/application.rb
config.autoload_paths += %W(#{config.root}/lib) # where lib is directory to survery_provider
If you use Rails 5, be careful that autoload is disabled in production environment. Check this link for more info.

"NameError, uninitialized constant" error after modifying controller code

I was able recently to organize my code by grouping everything into folders.
I had an issue with having the same "group name" for both my group of controllers under the app/ directory and my module under the lib/ directory but I was able to fix by following this:
Rails: Same name for a library module and a group of controllers?
I also know that whenever you change your lib code, you need to restart the rails server which is totally fine by me.
But after the recent re-organization, every time I change the code in the controllers, I get the following error!!!
NameError at /admin
uninitialized constant Admin::PagerDuty
and to resolve it, I simply restart the server!!
Any advice?!
EDIT: STRUCTURE:
Controller main_controller.rb is under app/controllers/admin
class Admin::MainController < ApplicationController
end
Helper main_helper.rb is under app/helpers/admin
module Admin::MainHelper
require "admin/pager_duty.rb"
def pager_duty
pagerduty = Admin::PagerDuty.new()
#on_call = pagerduty.on_call()
#counts = pagerduty.open_incidents()
end
end
lib pager_duty.rb is under lib/admin
module Admin
class PagerDuty
....
end
end
Try changing
require "admin/pager_duty.rb"
to
require_dependency "admin/pager_duty.rb"
in your module.

rails models

i have a model named test.rb and when i use #tests=Test.new in my controller i get the following error. Can someone temme how can i resolve this?
"undefined method `new' for Test:Module"
Looks like test is already the name of a module called Test if would seem that you have naming conflict. Try placing your own model in a module ie
module MyModule
class Test < ActiveRecord::Base
end
end
and then calling it like so
#test = MyModule::Test.new

Resources