NameError (uninitialized constant Api::V1::RegistrationsController::JsonWebTokenAuthentication): - ruby-on-rails

my jsonwebtokenauthentication.rb is in (app/lib.jsonwebtokenauthentication.rb):
class JsonWebTokenAuthentication
def some_method
#logic of the method
end
end
I am trying to access the above JsonWebTokenAuthentication method's in my registrations_controller.rb(app/controllers/api/v1/registrations_controller.rb)
class Api::V1::RegistrationsController < Api::V1::BaseController
def create
auth_token = JsonWebTokenAuthentication.some_method({user_id: user.id})
end
end
end
How can we use the class method specified inside lib folder in rails project.

Firstly if you want to use the RoR framework you are supposed to name your files using the Ruby Style guide. Also JsonWebTokenAuthentication looks more like a module, not a class for me, could you please clarify why you choose the class here?
I would suggest adding the some_method to the ApplicationController instead. Another option is to add a json_web_token_authentication.rb to the app/services/ but using the same namespace as the controller.

Related

Ruby on Rails 6 loading module

I would like to know what is the right way to use a class with namespace in rails 6.
I have the follow, but it isn't working and I'm receiving the error:
"Uninitialized constant ProductsController::Operations Did you mean? ProductsController::Options"
#app/operations/create.rb
module Operations
class Create
def self.foo
...
end
end
end
#app/controllers/products_controller.rb
class ProductsController < ApplicationController
def create
Operations::Create.foo
end
end
Can you help me please?
Your module should be inside of any folder. For example app/services/operations/create.rb (any name will work) with the same content you have:
module Operations
class Create
def self.foo
...
end
end
end
and call it Operations::Create.foo.
Also make sure you restart spring with spring stop.

undefined method for imported module

I have a controller under app/controllers
require_relative '../../lib/bases_helper'
class BasesController < ApplicationController
include BasesHelper
def index
BasesHelper.available_bases
end
end
I am trying to use a method defined in another module under lib:
module BasesHelper
def available_bases
#bases = Base.all
end
end
When I run my application and access the site, I get an error
undefined method `available_bases' for BasesHelper:Module
I can navigate to the method with my IDE by just clicking on its name. Why doesn't it resolve the method? What am I missing?
Although Junan Chakma answer will work, i will advise against setting it up that way. Its better (and follows Rails conventions) to use a private method in your controller and use a callback (i.e. before_action); for example:
class BasesController < ApplicationController
before_action :set_available_bases, only: [:index]
def index
end
private
def set_available_bases
#bases = Base.all
end
end
This will set up #bases instance variable to be used in your index action and index.html.erb view.
I think you don't need to add BasesHelper to use available_bases method. Just use method name like this
def index
available_bases
end
As you imported BasesHelper module in your controller all methods of BasesHelper will be available in your controller. So you can use those method just by calling(without its module name) its name.
If you want to improve your code quality and follow rails conventions then please check Gerry's answer.
It is because your method available_bases is an instance method of BasesHelper, not a class method. And you are calling it as if it were a class method.
If you want to use available_bases like a class method, extend the class instead of include-ing it.
class BasesController < ApplicationController
extend BasesHelper
...
end

How to use reusable controller methods in Rails?

I'm relatively new to Rails and am working on creating a simple user authentication system to get to grips with how Rails works.
I'm currently at the point where I'd like to create some methods that I can use in my controllers like so:
is_logged? # => true
and
current_user_id # => 6
These would be used to interact with sessions, mainly so I'm not repeating myself in the controller.
Where would I define these functions and how would I include them in a controller?
Thanks a lot in advance for any help.
Method 1
You can define these method in helper files, inside app/helpers/my_module.rb. You can create a module there, put all the methods inside of it, and then include the modules in your control to use these method.
module MyMoule
def is_logged?
...
end
end
Then in you class include the module
class MyClassController < ApplicationController
include MyModule
def my_method
#Use it like this
logged_in = MyModule.is_logged?
end
end
Method 2
If you using session related stuff you can always put them inside application_controller.rb. And since all your controller will inherit ApplicationController the methods will be available to you.
class ApplicationController < ActionController::Base
def is_logged?
...
end
end
In your other controller you can use them directly.
class MyClassController < ApplicationController
def my_method
logged_in = is_logged?
end
end

Using helper in controller in Rails 4.2.4

I'm confused by the rails documentation that I'm reading here. In particular, this sentence:
By default, each controller will include all helpers. These helpers
are only accessible on the controller through .helpers
What is this .helpers that it is referring to? I have a helper defined in app/helpers/areas_helper.rb:
module AreasHelper
def my_helper
puts "Test from helper"
end
end
I would like to use this helper in app/controllers/locations_controller.rb:
class LocationsController < ApplicationController
def show
helpers.my_helper
end
end
However, I get a method undefined error. How is this .helpers supposed to be used?
I know there are other ways to get access to helpers in controllers, but I'm specifically asking about this piece of documentation and what it's trying to say.
You're meant to include the helper class in the controller:
#app/controllers/locations_controller.rb
class LocationsController < ApplicationController
include AreasHelper
def show
my_helper
end
end
This feature was introduced in Rails 5 with following PR
https://github.com/rails/rails/pull/24866
So, we can use this feature from Rails 5 and onwards and not in Rails 4.x.

Using wrong namespace in the Rails controller

I have a controller: "/app/controllers/analyst/test_orders_controller.rb".
In this file I have:
class Analyst::TestOrdersController < ApplicationController
def new
#order = Order.new
end
end
But I have an error:
uninitialized constant Analyst::TestOrdersController::Order
But I don't want to use Analyst::TestOrdersController::Order.new, I just want to use Order.new. It is strange. What is the problem?
Use ::Order.new
The interpreter is looking for the definition of Order under the Analyst module namespace, this happens because the application models are loaded lazily so the file models/order.rb has not been read yet. Adding the general namespace tells it search for the definition in the Rails paths.
The way to confirm this is to add some random function call in the Order model body and see that it's not executed unless you call ::Order explicitly.
try:
module Analyst
class TestOrdersController < ApplicationController
def new
#order = Order.new
end
end
end
I think it has to do with module nesting:
https://cirw.in/blog/constant-lookup

Resources