0

I have an professional model and a citizen model. What i am trying to do is a nested citizen registration inside the professional model, but i am stumbling a lot...

i was using in my routes:

  get "professional/citizen_edit/:id" => "professional#citizen_edit", as: :professional_citizen_edit
  get "professional/cidadao_show/:id" => "professional#citizen_show", as: :professional_citizen_show
  put "profissionais/cidadao_update/:id" => "professional#citizen_update", as: :professional_citizen_update

And using custom controller actions everywere And was almost done, but i had got stuck when trying to add a button for a new citizen (my form page was stuck in the update action and i cant get it to work with both actions)

While trying to solve this issue, i found this http://guides.rubyonrails.org/routing.html and this:

  namespace :professionals do
    resources :registrations
  end

Looked a more elegant way to deal with my problem. But now i dont know what to do with the controller! I got these routes:

professionals_registrations GET    /professionals/registrations(.:format)                           professionals/registrations#index
POST   /professionals/registrations(.:format)                           professionals/registrations#create
new_professionals_cadastro GET    /professionals/registrations/new(.:format)                       professionals/registrations#new
edit_professionals_cadastro GET    /professionals/registrations/:id/edit(.:format)                  professionals/registrations#edit
professionals_cadastro GET    /professionals/registrations/:id(.:format)                       professionals/registrations#show
PUT    /professionals/registrations/:id(.:format)                       professionals/registrations#update
DELETE /professionals/registrations/:id(.:format)                       professionals/registrations#destroy

And i tried to add a custom controler "registrations" either inside app/controllers or app/controllers/professionals but i cant find the controler right path and keep getting

at: http://127.0.0.1:3000/Profissionais/registrations)
uninitialized constant Profissionais::RegistrationsController

Anyone have any clue of what to do now?

1 Answer 1

1

When using namespaces you have to do two things:

  • Move all controllers to a folder under app/controllers/ with the same name as your namespace
  • Let your controllers inherit from a controller with the same name of your namespace (in your case Profissionais)

The reason why you are getting that error is that the parent controller Profissionais does not exist (i.e. uninitialized constant)

As the documentation states, all your controllers must be under a namespace (in your case Profissionais::) as you defined it in routes.rb.

HINT:

I haven't tried it myself but it may also be possible to use modules to create your namespace instead of inheriting from a parent controller residing in app/controllers/. For more see here

Sign up to request clarification or add additional context in comments.

4 Comments

I also named the registrations controller just registrations.rb instead registrations_controller.rb But still getting Expected [long PATH]app/controllers/profissionais/registration_controller.rb to define Profissionais::RegistrationController. How the inherit should look like in class definition?
I think it is: "class Profissionais::RegistrationController < ApplicationController" But now i get: NameError at /profissionais/registrations uninitialized constant Registration
You have to inherit from profissionais which in turn inherits from application controller. Note that you have to create class profissionais in order to be able to inherit from it.
Nevermind. Wrong stuff write on the controller. Fixed!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.