Rails: Uninitialized constant - can not access to another class from controller - ruby-on-rails

search_handlers_controller.rb
class SearchHandlersController < ApplicationController
def match
#string = 'string'
#result = case params['search_style']
when 'match'
MatchService.call(#string)
...
MatchService.rb in /services:
# frozen_string_literal: true
class MatchService < ApplicationService
...
def call(string)
'Match'
end
end
Error when calling MatchService from SearchHandlersController#match:
NameError (uninitialized constant SearchHandlersController::MatchService):
app/controllers/search_handlers_controller.rb:18:in `match'

First of all, if you named the file MatchService.rb, you should change name of this file into match_service.rb.
If this isn't work in your case, you can always call the service from the root by add :: before the service name like that: ::MatchService.call(#string).
I hope it helps you!

Related

Undefined module method in model

lib/modules/file_type.rb
module Modules
module Type
def friend_name(type:)
...
end
end
end
app/models/car.rb
class Car < ApplicationRecord
include Modules::Type
def self.to_array
...
name = friend_name(type: 'test')
...
end
end
But I am getting this error:
undefined method `friend_name'
I am not sure why I am getting this error.
Anyone can help me?
If friend_name is a class method then instead of include use extend in Car model
extend Modules::Type
More info about difference between include and extend could be found here -
What is the difference between include and extend in Ruby?
Hope that helps!

Uninitialized constant error when routing partials according to their slugs

I want to refactor a view with the following pattern:
View code:
<%= render #static_page.render_nested_partial %>
Model code:
def render_nested_partial
return FormPartialResolver.new(self).resolve_path
end
Parent code:
# partial_resolver/form_partial_resolver.rb
class FormPartialResolver
def initialize(static_page)
#static_page = static_page
end
# It should return something like "FormPartialResolver::Home"
def resolve_path
"FormPartialResolver::#{#static_page.slug.underscore.camelize}".constantize.new(#static_page).partial_path
end
def partial_path
# it should return the path "form_partials/home_wrap"
return "form_partials/#{#static_page.slug.underscore}_wrap"
end
end
Child code:
# partial_resolver/home.rb
module PartialResolver
class Home < FormPartialResolver
def initialize(static_page)
super(static_page)
end
end
end
This produces the following error:
Showing project/app/views/admin/static_pages/_form.html.erb where line #33 raised:
uninitialized constant FormPartialResolver::Home8d0bdef24dc34f8bAb1c006feeb02845
def resolve_path
"FormPartialResolver::#{#static_page.slug.underscore.camelize}".constantize.new(#static_page.slug).> partial_path
end
I guess I'm not passing the right parameter.
What could I do to have just "Home"?
This code...
module PartialResolver
class Home < FormPartialResolver
defines a module PartialResolver, containing a class Home. You can reference it using PartialResolver::Home, not FormPartialResolver::Home.
Your code attempts to use FormPartialResolver::Home, which is not defined anywhere we can see here. Your Home class inherits from FormPartialResolver, but that has nothing to do with how you reference the class, rather, you use the containing modules and classes (PartialResolver in this case) and the scope resolution operator, ::.
You can fix this by updating your resolve_path to return the correct string representing your module and class:
# It should return something like "PartialResolver::Home"
def resolve_path
"PartialResolver::#{#static_page.slug.underscore.camelize}"...
end

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.

Using constant in model

I'm trying to define a constant in an initializer file and to use it into a model.
config/initializers/constants.rb
DEFAULT_EVENT_DURATION = 15
app/models/event.rb
class Event < ActiveRecord::Base
before_validation :set_end_and_allday
[...]
def set_end_and_allday
self.allDay ||= false
self.end_event ||= self.start + DEFAULT_EVENT_DURATION.minute
end
end
However, when it try to create a new event, it displays the following error in the logs:
NameError - uninitialized constant Event::DEFAULT_EVENT_DURATION
Am I doing something wrong?
I've made some searches on google, but I didn't find any solution (except defining constant into the model and not in the initializer... and that's not what i want to do).
It was just a scope problem: constant was in the root scope but it was searching the constant in the controller scope.
A simple '::DEFAULT_EVENT_DURATION' solved the issue.

Getting "uninitialized constant" in Rails app

I'm new to Rails and feeling my way, but this has me stumped.
I moved some constants to a separate module ie:
module Fns
Fclick = "function() { alert(\"You clicked the map.\");}\n"
...
end
then in my controller added:
require "fns"
class GeomapController < ApplicationController
def index
fstring = Fns::Fclick
...
end
but when I run the server I get:
uninitialized constant Fns::Fclick
what am I missing?
Works for me if the module is in lib/fns.rb.
For the sake of convention use all-caps for constant names: http://itsignals.cascadia.com.au/?p=7

Resources