NameError uninitialized constant JobsController::Pagination using no gems in Ruby - ruby-on-rails

Using Rails 6.1.6 and Rails 2.7.0. and am following an article for adding pagination in Ruby using no gems. I'm running into a NameError exception for my Pagination module. Not certain if the naming of the hierarchy in the module is incorrect.
Pagination Helper
module PaginationHelper
def paginate(collection:, params: {})
pagination = Services::Pagination.new(collection, params)
[
pagination.metadata,
pagination.results
]
end
end
Jobs Controller
class JobsController < ApplicationController
include Pagination
JOBS_PER_PAGE = 8
def index
#pagination, #jobs = paginate(collection: Job.all, params: page_params)
end
Error Message
NameError Exception
I've tried restarting the server to no avail. Am I missing something in the Pagination module?

That should be include PaginationHelper in your controller.
Or rename the helper to module Pagination.

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

Class and module scope in rails helpers

I'm trying to write a rails app that creates an object in the controller based on a helper module, which is written below:
module StockPricesHelper
require 'net/http'
class Stock
attr_accessor(:data)
def initialize(stock)
#url = "http://finance.yahoo.com/d/quotes.csv?s=#{stock}&f=sb2b3jk"
end
def download_data
#data = NET::HTTP.get_response(URI.parse(#url)).body
end
def clean_string
#data = #data.strip
end
def db_format
1
end
end
end
I get an error uninitialized constant StockPricesHelper::Stock::NET from the rails server.
Am I correctly putting this in a helper module?
What am I doing wrong? I think I'm off on the scope but I don't know where.
You have misspelled the "NET" module. It is Net. (Ruby is case sensitive)
Rails helpers are intended to be view helpers, i.e. aid in generating HTML.
It looks like you are performing something which would be better placed in a controller or background job.

Create bare rails controller class

I'm trying to create clean controller based on ActionController::Base. That's what I try:
class MetalController
ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left|
include left
end
end
From Rails doc:
Shortcut helper that returns all the modules included in
ActionController::Base except the ones passed as arguments:
This gives better control over what you want to exclude and makes it
easier to create a bare controller class, instead of listing the modules
required manually.
My another controller inherits from MetalController :
class API::BaseController < MetalController
#.... my awesome api code
end
So this not work then i launch rails server:
block in <module:AssetPaths>': undefined methodconfig_accessor' for
MetalController:Class (NoMethodError)
Rails 4.1.0, Ruby 2.1.0
Update:
If i include ActiveSupport::Configurable
throws the errors:
_implied_layout_name': undefined local variable or method
controller_path' for MetalController:Class (NameError)
You need to inherit from ActionController::Metal:
class MetalController < ActionController::Metal
ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left|
include left
end
end

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

Using sanitize within a Rails controller

I'm trying to call sanitize within a controller. Here's what I tried:
class FooController < ApplicationController
include ActionView::Helpers::SanitizeHelper
# ...
end
However, I'm getting this error:
undefined method `white_list_sanitizer' for FooController:Class
I searched around and people recommended switching the include line to include ActionView::Helpers, but that results in this error:
undefined method `url_for' for nil:NilClass
What's the correct way to call sanitize? I'm using Rails 2.3.5.
you can use this ActionController::Base.helpers inside action method:
class SiteController < ApplicationController
def index
render :text => ActionController::Base.helpers.sanitize('<b>bold</b>')
end
end
Hope this helps
Rails 6:
To strip links (for example) from a text, just call:
...
Rails::Html::LinkSanitizer.new.sanitize("links here will be stripped")
...
see https://github.com/rails/rails-html-sanitizer
I'm not sure what you're trying to do here but I'm almost 100% certain it doesn't belong in the controller.
If you want to sanitize an attribute before you save it to the DB, do so in the model with a before save callback.
Otherwise, sanitize in the view template or view helper.

Resources