TypeError: Invalid argument type Class (expected module) with control

So I have this controller named guest_controller.rb that looks like this:

class GuestsController < ApplicationController
    include Data

    def index
        display_data("db")
        display_data("session")
    end

    def new
        @guest = Guest.new
    end

    def create
        case params[:option]
            when '0'
                logger.debug 'Salvestame andmebaasi'
                save_data("db")
            when '1'
                logger.debug 'Salvestame sessiooni'
                save_data("session")
        end
    end

    private
    def guest_params
        params.require(:guest).permit(:name, :text)
    end
end

      

And also I have a problem named data.rb in controllers / concens /:

module Data
    extend ActiveSupport:Concern

    def save_data(save_method)
        case save_method
            when "db"
                @guest = Guest.new(guest_params)
                if @guest.save
                    flash[:success] = "New guest entry created!"
                    redirect_to guests_path
                else
                    render 'new'
                end
            when "session"
                name = params[:guest][:name].to_s
                text = params[:guest][:text].to_s

                @guests_session = session[:guests]

                if session[:guests].nil?
                    @guests_session = [{:name => 'temp', :text => 'temp'}]
                else
                    logger.debug @guests_session
                    @guests_session.push({:name => name, :text => text})
                end

                session[:guests] = @guests_session
                redirect_to guests_path
        end
    end

    def display_data(display_method)
        case display_method
            when "db"
                @guests = Guest.all
            when "session"
                if session[:guests].nil?
                    @guests_session = [{:name => 'temp', :text => 'temp'}]
                else
                    #@guests_session = [{:name => 'temp', :text => 'temp'}]
                    @guests_session = session[:guests]
                end
        end
    end
end

      

Now, when I open the corresponding view, I get this error:

TypeError in index UserController # invalid argument type Class (expected module)

With stacktrace:

app/controllers/guests_controller.rb:2:in `include'
app/controllers/guests_controller.rb:2:in `<class:GuestsController>'
app/controllers/guests_controller.rb:1:in `<top (required)>'
activesupport (4.1.5) lib/active_support/dependencies.rb:443:in `load'
activesupport (4.1.5) lib/active_support/dependencies.rb:443:in `block in load_file'
activesupport (4.1.5) lib/active_support/dependencies.rb:633:in `new_constants_in'
activesupport (4.1.5) lib/active_support/dependencies.rb:442:in `load_file'
activesupport (4.1.5) lib/active_support/dependencies.rb:342:in `require_or_load'
activesupport (4.1.5) lib/active_support/dependencies.rb:480:in `load_missing_constant'
activesupport (4.1.5) lib/active_support/dependencies.rb:180:in `const_missing'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:238:in `const_get'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:238:in `block in constantize'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:236:in `each'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:236:in `inject'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:236:in `constantize'
activesupport (4.1.5) lib/active_support/dependencies.rb:552:in `get'
activesupport (4.1.5) lib/active_support/dependencies.rb:583:in `constantize'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:78:in `controller_reference'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:68:in `controller'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:46:in `call'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:678:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.5) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.5) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.5) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.5) lib/rails/engine.rb:514:in `call'
railties (4.1.5) lib/rails/application.rb:144:in `call'
passenger (4.0.50) lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
passenger (4.0.50) lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
passenger (4.0.50) lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
passenger (4.0.50) lib/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'

      

I am just trying to learn how to take advantage of controller problems. I want to detach a large chuck of my code and tune it in order to keep the controller clean and clean.

There isn't much to find when googling or searching SO for controller problems, so I asked this new question hoping to get some help.

So how can I fix this problem and what am I doing wrong?

+3


source to share


2 answers


You ran into a naming conflict - Data

class exists on the Ruby keyline: here . This means that Rails does not initiate const_missing and your problem will never be loaded. Rename your issue to DateModule or similar.



+8


source


You made a simple typo in your Data problem - it should be extend ActiveSupport::Concern

(note the double colon, which is the namespace separator in Ruby).



+1


source







All Articles