0

I'm learning rails and got this small app for learning purposes.

This is my folder structure, I'm trying to customize it for a DDDish approach:

enter image description here

This is my launcher_controller:

module Web
    class LauncherController < ApplicationController
        def launch
            requestBody = params[:launcher]

            render json: requestBody
        end
    end
end

And the routes.rb:

Rails.application.routes.draw do
    scope module: 'web' do
        post 'launcher/launch', action: 'launch', controller: 'launcher'
    end
end

I already added the web and controllers path to the autoload_paths for zeitwerk to load the files in application.rb:

config.autoload_paths << config.root.join('app', 'web')
config.autoload_paths << config.root.join('app', 'web', 'controllers')

I can fire rails server up but when I call the controller's action I get:

ActionController::RoutingError (uninitialized constant Web)

I feel like this should be something really simple but I can't find the problem... I'm following the docs at https://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use

So what basic concept am I missing here? Am I not defining the module at the launch_controller.rb file?

EDIT

Ok, based on the first answern I changed the module to Web in the module definition and 'web' on the routes.rb.

Now I got

ActionController::RoutingError (uninitialized constant Web)

I don't know if this could be a problem, but this is a web API only app. I generated the project with the following command:

rails new tracker-launcher -M -O -J -T --api
4
  • In your file tree, you have web as the directory. But in the file you have WebStuff and in the routes you have :web_stuff. It can't find the correct director bc you named it differently Mar 9, 2020 at 17:11
  • @Int'lManOfCodingMystery Just updated the question with what you suggested. Even when the folder name and the module name match, I still get the same error.
    – James Braz
    Mar 9, 2020 at 23:35
  • 1
    I think you may also need to move the web directory inside the controllers directory and spearate the controllers there. And then if you need any other separation for your web scope, then create a directory in each main directory, ie models, services, etc. Mar 10, 2020 at 8:24
  • @Int'lManOfCodingMystery Yeah, I'm getting to the same conclusion. Better not mess with rails base folders and do whatever namespacing you need inside them, for controllers at least. Other namespaces inside the folders I created inside the app folder works just fine.
    – James Braz
    Mar 10, 2020 at 11:10

1 Answer 1

1

There could be many reasons for it. First thing I see is you defined WebStuff, but in routes using webstuff, which is Webstuff (see case). Try set it to web_stuff or rename classes/modules. Also, file names are important for Autoloading and Reloading Constants (Classic Mode) or Autoloading and Reloading Constants (Zeitwerk Mode).

2
  • The docs example won't match the case of the module with that of the scope in the routes file. guides.rubyonrails.org/…
    – James Braz
    Mar 9, 2020 at 23:38
  • But I tried what you suggested anyway, if I match the module case, then rails can't find the folder web. I get the error: 'Web/launcher' is not a supported controller name. This can lead to potential routing problems.
    – James Braz
    Mar 9, 2020 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.