Method undefined for modules in rails 4? - ruby-on-rails

File structure:
../controllers
/api
/v1
users_controller.rb
some_controller.rb
Inside users_controller.rb
module Api
module V1
class UsersController < ApplicationController
def create
return false
end
end
end
end
I can include Api in a controller and do Api::V1::UsersController. However, when I try
Api::V1::UsersController.create
in any controller I get an error:
undefined method `create' for Api::V1::UsersController:Class
I've tried doing modules in lib, but the rails 4 autoloading was being weird so I tried doing it this way, but I don't know why my methods are undefined. When I go into the console and puts Api::V1::UsersController.methods.sort, the :create method isn't there. So what am I doing wrong?

create is not a class method. It can't be called as Class.method.
You need an instance of this class to call it.
If you just want to try(though this is not the way controller work)
Api::V1::UsersController.new.create

Related

NameError (uninitialized constant Api::V1::RegistrationsController::JsonWebTokenAuthentication):

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.

How can I create controller instance with params from another controller?

I need to create a controller instance from another controller for using its methods. When I creating
c = SomeController.new
c.some_method
some_method use params[], and then I have an error NoMethodError: undefined method 'parameters' for nil:NilClass.
How can I pass params to the controller?
The thing you are trying to do it not recommended any framework. You probable have some code that you wanted in multiple controllers. To achieve your desired behavior, extract the common code to library can call that in any controller you want.
You don't instantiate controllers in Rails - its done by the router when it matches a request to a route.
Its just not done because it violates the way MVC in Rails works - one request = one controller. You also need to pass the entire env hash from rack into the controller for it even work properly.
But really - don't. There are much better ways.
If you need to share a method between controllers there are better ways such as using inheritance:
class ApplicationController
def some_method
end
end
class FooController < ApplicationController
end
class BarController < ApplicationController
end
Or mixins:
# app/controllers/concerns/foo.rb
module Foo
def some_method
end
end
class BarController < ApplicationController
include Foo
end
class BazController < ApplicationController
include Foo
end
If you instead need to move a request from one controller action to another you should be redirecting the user.

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

Rails using ActionView::Helper methods outside of view

I've created a helper which I'd like to use for text manipulation
module ApplicationHelper
module TextHelper
extend ActionView::Helpers
end
end
However when I run ApplicationHelper::TextHelper.simple_format "foo" in Rails console I get
NoMethodError: undefined method `white_list_sanitizer' for Module:Class
Is there anything else I need included?
I have already looked at https://github.com/rails/rails/issues/13837 but it didn't work.
Using Rails 4, Ruby 1.9.3
If you're in the console, you should be able to just do helper.simple_format('hi'). The helper method is available in console as a way to call some helper methods.
When using a custom helper:
# app/helpers/custom_helper.rb
module CustomHelper
def custom_method(x)
puts "Custom method #{x}"
end
end
# from the console
helper.custom_method('hi')
# from the controller
class SomeController < ApplicationController
def index
view_context.custom_method('hi')
end
end

Ruby On Rails - Using concerns in controllers

Possible Noob Warning: New to RoR
I am trying to use concerns in RoR. Right now I just have a very simple concern writen
#./app/controllers/concerns/foo.rb
module Foo
extend ActiveSupport::Concern
def somethingfoo
puts "Ayyyy! Foo"
end
end
When I try and use this concern in my controller I get a undefined method error
#./app/controllers/foo_controller.rb
class FooController < ApplicationController
include Foo
def show
Foo.somethingfoo # undefined method 'somethingfoo' for Foo:Module
render plain: "Ohh no, It doesnt even show me because of the error above me"
end
end
To my knowledge somethingfoo should be called but it is not. I have also tried defining somethingfoo in a included do ... end block in the concern but this does not work either.
Is there something I am missing? Can concerns not be used like this with controllers?
If you include modules (extended by ActiveSupport::Concern or not), the methods of that module become instance methods of the including class/module.
Your Controller method should hence read
def show
somethingfoo
render plain: "Yeah, I'm shown!"
end

Resources