Rails 5 - nested controllers with routes - ruby-on-rails

I'm trying to make an app in Rails 5.
In order to keep the file structure neat, I'm trying to make folders inside my controllers directory, that I can use to group similar resources.
For example, I have:
app/controllers/users/users_controller.rb
I can then make my sessions controller nested inside the controllers/users director so that all resources relating to the user are grouped under the user folder.
I'm stuck though for what to do with my routes file.
When I rake routes, I can see:
users#index {:controllers=>{:users=>"users/users"}}
POST /users(.:format) users#create {:controllers=>{:users=>"users/users"}}
new_user GET /users/new(.:format) users#new {:controllers=>{:users=>"users/users"}}
edit_user GET /users/:id/edit(.:format) users#edit {:controllers=>{:users=>"users/users"}}
user GET /users/:id(.:format) users#show {:controllers=>{:users=>"users/users"}}
PATCH /users/:id(.:format) users#update {:controllers=>{:users=>"users/users"}}
PUT /users/:id(.:format) users#update {:controllers=>{:users=>"users/users"}}
DELETE /users/:id(.:format) users#destroy {:controllers=>{:users=>"users/users"}}
In my routes file, I've tried a few things (set out below) - none of them work:
Rails.application.routes.draw do
devise_for :users,
:controllers => {
:sessions => 'users/sessions',
:registrations => "users/registrations",
:omniauth_callbacks => 'users/omniauth_callbacks'
}
resources :identities,
:controllers => {
:identities => 'users/identities'
}
resources :users do
scope module: :users do
resources :users
end
end
root 'home#index'
end
I have also tried:
resources :users,
:controllers => {
:users => 'users/users'
}
Each time, I get an error that says:
ActionController::RoutingError at /users
uninitialized constant UsersController
I don't know what I need to do to get this working. I have changed each of my controllers that is nested inside the controllers/users folder with a prefix of:
Users::
Can anyone see how to set this up so that I can keep my files neatly organised?
Note: I haven't created the same file directory structure in my models folder. I want to - but I'm concerned that I'm not able to figure this out for the controllers. Can anyone see what I'm doing wrong?

I recommend you put the actual users_controller in the base controllers directory, and only put the nested controllers inside the users directory (ie follow the same structure as the nesting).
The alternative is to name the users_controller the way that rails is expecting ie inside the Users module:
module Users
class UsersController < ApplicationController
...
and then refer to it with: Users::UsersController
I always find the duplication in the name a bit cumbersome, and prefer top-level controllers to just be in the base directory.

I also really like the approach explained in this video:
https://youtu.be/1B0Vioz4Ukw
Basically he creates BaseController for each Module, those inherit from ApplicationController and then the related controllers inherit directly from their corresponding BaseController.
Example used in video
Controllers
Controllers are namespaced, in the example with Auth. Then a base class is made, derived from ApplicationController. The base class is then used for all other of the Auth controllers.
# app/controllers/auth/base_controller.rb
class Auth::BaseController < ApplicationController
end
# app/controllers/auth/posts_controller.rb
class Auth::PostsController < Auth::BaseController
...
end
# app/controllers/auth/sessions_controller.rb
class Auth::SessionsController < Auth::BaseController
...
end
# app/controllers/auth/users_controller.rb
class Auth::UsersController < Auth::BaseController
...
end
Routes
Resources are namespaced while simple routes have auth/ added to their routes.
# config/routes.rb
namespace :auth do
resource :confirmation
resource :session, only: [:create]
resources :users, only: [:create]
end
get 'login' => 'auth/sessions/new'
delete 'logout' => 'auth/sessions#destroy'
get 'register' => 'auth/users#new'
Views
Views are now namespaced under auth.
app/views/auth/sessions/*
app/views/auth/posts/*
app/views/auth/users/*
Paths
Paths will also be under auth namespace to work. Check rails routes for details.
Example route: auth_session_path
Example route from set of objects: [:auth, #user]

Related

Rails 6 - Issue with nested resources in a namespace

So I am trying to nest resources under a namespace, however when i try to navigate to the UserProfile new page I am hitting the following error:
ActionController::RoutingError at /users/xxxxxx/user_profiles/new
uninitialized constant AccountManagementPages::UserProfilesController
Did you mean? AccountManagementPages::UsersController
This is how the resources are set up in my routes.rb file
constraints(AccountManagement) do
namespace :account_management_pages, path: '' do
root to: 'users#new', as: :registration
resources :users, except: %w[index], path_names: { new: 'register' } do
resources :user_profiles
end
end
end
my file structure for both my controller and views are configured correctly (at least I thought they were).
And here is how my views are nested.
This is how I have my user_profiles_controller configured:
module AccountManagementPages
module Users
class UserProfilesController < ApplicationController
def show; end
def new; end
def edit; end
def create; end
def update; end
end
end
end
and my model associations (don't think this is overly relevant here but just incase it is.)
class User < ApplicationRecord
has_one :user_profile, dependent: :destroy
end
class UserProfile < ApplicationRecord
belongs_to :user
end
any help here would be greatly appreciated. Not sure why I am hitting this error?
Thanks in advance.
If you do rails routes, you'll get (amongst other things):
Prefix Verb URI Pattern Controller#Action
account_management_pages_registration GET / account_management_pages/users#new
account_management_pages_user_user_profiles GET /users/:user_id/user_profiles(.:format) account_management_pages/user_profiles#index
POST /users/:user_id/user_profiles(.:format) account_management_pages/user_profiles#create
new_account_management_pages_user_user_profile GET /users/:user_id/user_profiles/register(.:format) account_management_pages/user_profiles#new
edit_account_management_pages_user_user_profile GET /users/:user_id/user_profiles/:id/edit(.:format) account_management_pages/user_profiles#edit
account_management_pages_user_user_profile GET /users/:user_id/user_profiles/:id(.:format) account_management_pages/user_profiles#show
PATCH /users/:user_id/user_profiles/:id(.:format) account_management_pages/user_profiles#update
PUT /users/:user_id/user_profiles/:id(.:format) account_management_pages/user_profiles#update
DELETE /users/:user_id/user_profiles/:id(.:format) account_management_pages/user_profiles#destroy
account_management_pages_users POST /users(.:format) account_management_pages/users#create
new_account_management_pages_user GET /users/register(.:format) account_management_pages/users#new
edit_account_management_pages_user GET /users/:id/edit(.:format) account_management_pages/users#edit
account_management_pages_user GET /users/:id(.:format) account_management_pages/users#show
PATCH /users/:id(.:format) account_management_pages/users#update
PUT /users/:id(.:format) account_management_pages/users#update
DELETE /users/:id(.:format) account_management_pages/users#destroy
As you can see, user_profiles is not nested under the users namespace. Rails, therefore, is expecting:
module AccountManagementPages
class UserProfilesController < ApplicationController
...
end
end
If you do:
constraints(AccountManagement) do
namespace :account_management_pages, path: '' do
root to: 'users#new', as: :registration
resources :users, except: %w[index], path_names: { new: 'register' } do
scope module: :users do
resources :user_profiles
end
end
end
end
...and then rails routes, you get (amongst other things):
Prefix Verb URI Pattern Controller#Action
account_management_pages_registration GET / account_management_pages/users#new
account_management_pages_user_user_profiles GET /users/:user_id/user_profiles(.:format) account_management_pages/users/user_profiles#index
POST /users/:user_id/user_profiles(.:format) account_management_pages/users/user_profiles#create
new_account_management_pages_user_user_profile GET /users/:user_id/user_profiles/register(.:format) account_management_pages/users/user_profiles#new
edit_account_management_pages_user_user_profile GET /users/:user_id/user_profiles/:id/edit(.:format) account_management_pages/users/user_profiles#edit
account_management_pages_user_user_profile GET /users/:user_id/user_profiles/:id(.:format) account_management_pages/users/user_profiles#show
PATCH /users/:user_id/user_profiles/:id(.:format) account_management_pages/users/user_profiles#update
PUT /users/:user_id/user_profiles/:id(.:format) account_management_pages/users/user_profiles#update
DELETE /users/:user_id/user_profiles/:id(.:format) account_management_pages/users/user_profiles#destroy
account_management_pages_users POST /users(.:format) account_management_pages/users#create
new_account_management_pages_user GET /users/register(.:format) account_management_pages/users#new
edit_account_management_pages_user GET /users/:id/edit(.:format) account_management_pages/users#edit
account_management_pages_user GET /users/:id(.:format) account_management_pages/users#show
PATCH /users/:id(.:format) account_management_pages/users#update
PUT /users/:id(.:format) account_management_pages/users#update
DELETE /users/:id(.:format) account_management_pages/users#destroy
...and user_profiles will be nested under users. And you should be able to use:
module AccountManagementPages
module Users
class UserProfilesController < ApplicationController
...
end
end
end

Create root and routes for Devise object

I have created an Admin model with the Devise gem. Using the Devise controller generator, I now have a app/controllers/admins folder containing all the stock controllers for me to modify if I choose, such as sessions_controller, passwords_controller, etc.
However, I can't figure out how to just get an Admin controller and simple admin routes like admin_path or new_admin_path.
Here's my rake routes | grep admin
new_admin_session GET /admin/sign_in(.:format) admins/sessions#new
admin_session POST /admin/sign_in(.:format) admins/sessions#create
destroy_admin_session DELETE /admin/sign_out(.:format) admins/sessions#destroy
new_admin_password GET /admin/password/new(.:format) devise/passwords#new
edit_admin_password GET /admin/password/edit(.:format) devise/passwords#edit
admin_password PATCH /admin/password(.:format) devise/passwords#update
PUT /admin/password(.:format) devise/passwords#update
POST /admin/password(.:format) devise/passwords#create
admin_root GET /admin(.:format) admins/sessions#portal
admin_sign_out GET /admin/sign_out(.:format) admin/sessions#destroy
And here are the relevant parts of my routes.rb
devise_for :admins, path: 'admin', controllers: { sessions: 'admins/sessions' }
devise_scope :admin do
get "/admin", to: 'admins/sessions#portal', as: 'admin_root'
get "/admin/sign_out", to: 'admin/sessions#destroy', as: 'admin_sign_out'
end
You'll see that I've currently got a portal method in my Admin::SessionsController, which is my current workaround. I know the right place for that page is in an AdminsController but I can't figure out how to set that up.
Adding admins: 'admins/admins' to the devise_for :admins, controllers: block doesn't give me any new routes. I tried adding an AdminsController with methods but that doesn't help either, trying to go to /admin/new or /admins/new says no route matches.
This is how I have my device and namespaces set up
# config/routes.rb
Rails.application.routes.draw do
devise_for :admins, :controllers => { registrations: 'admins/registrations',
sessions: 'admins/sessions',
passwords: 'admins/passwords',
confirmations: 'admins/confirmations'
}
authenticate :admin do
namespace :admins do
...
root 'dashboards#index'
end
end
...
root 'pages#index'
Now controllers are also important.
#app/controllers/admin_controller.rb
class AdminController < ApplicationController
layout 'admins/application'
before_filter :authenticate_admin!
end
The Devise controllers I have them set like this
#app/controllers/admins/sessions_controller.rb
class Admins::SessionsController < Devise::SessionsController
layout 'admins/application'
...
end
Repeat this process for all your other device controllers
Let me know if this helps you out

How to nest a namespaced ruby on rails controller

I have a controller in a name spaced controller/user directory, so its first line is
class User::BookingsController < ApplicationController
I have set the routes as follows
resources :users do
namespace :user do
resources :bookings
end
end
The path to index action is
user_user_bookings_path or
/users/:user_id/user/bookings(.:format)
which works fine, but the double user_user sounds like a Catch 22 joke. Is there a more elegant way to do this?
What would you like to achieve? You are namespacing under "user" after all. You can try options like as: nil, but I think this will be even funnier (user__bookings_path).
What you can do is write every route by hand, something like:
resources :users do
post 'bookings', to: 'user/bookings#create'
end
# => user_bookings_path, POST /users/:user_id/bookings
or if you want to preserve the URL
resources :users do
post '/user/bookings', to: 'user/bookings#create', as: 'bookings'
end
# => user_bookings_path, POST /users/:user_id/user/bookings
Try this
resources :users do
resources :bookings
end
According to the docs you can namespace your resource
by using the namespace block
namespace :api do
resources :users
end
this would give you those routes:
/api/users [GET, POST]
/api/users/:id [GET, PUT, DELETE]
However you want to nest the resource within another resource (docs) you would do this:
resources :users do
resources :bookings
end
Which would result in these routes:
/users/:user_id/bookings [GET, POST]
/users/:user_id/bookings/:id [GET, PUT, DELETE]
Because you have your controller scoped under User you have to set the user scope like this:
resources :users do
resources :bookings, module: :user
end
Which results in those routes:
➜ playground rake routes
Prefix Verb URI Pattern Controller#Action
user_bookings GET /users/:user_id/bookings(.:format) user/bookings#index
POST /users/:user_id/bookings(.:format) user/bookings#create
new_user_booking GET /users/:user_id/bookings/new(.:format) user/bookings#new
edit_user_booking GET /users/:user_id/bookings/:id/edit(.:format) user/bookings#edit
user_booking GET /users/:user_id/bookings/:id(.:format) user/bookings#show
PATCH /users/:user_id/bookings/:id(.:format) user/bookings#update
PUT /users/:user_id/bookings/:id(.:format) user/bookings#update
DELETE /users/:user_id/bookings/:id(.:format) user/bookings#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy

Custom actions using devise

Background:
The Rails 4 application I am working on has differing logic for API and web registrations, and part of this logic makes their integration extremely difficult. To that end, I'm attempting to separate the routes to deal with issues arising from inheritance, new reCAPTCHA gem, and new logic. (both actions call registrations#create after their respective logic.) I've solved most of the issues arising from separating these two; however, getting the routes working has proven difficult as well.
I'd greatly appreciate any help!
Desired Result:
I'm trying to define a route to a custom action using Devise, and prevent it from creating the default route as well. I've gotten one of them working, but not the second. Here's the excerpt from my routes.rb:
Registry::Application.routes.draw do
devise_for :user,
controllers: {
passwords: 'users/passwords',
sessions: 'users/sessions',
registrations: 'users/registrations'
}
devise_scope :user do
post 'users', to: 'users/registrations#custom_one'
end
# ...
namespace :api do
namespace :v1 do
# ...
devise_scope :user do
post 'users', to: 'registrations#custom_two'
end
end
end
end
Issues:
The issue is that this code generates two nearly-identical routes. Excerpt from rake routes:
user_registration POST /users(.:format) users/registrations#create
users POST /users(.:format) users/registrations#custom_one
api_v1_users POST /api/v1/users(.:format) api/v1/registrations#custom_two
I also want the custom route to have the correct prefix/route name (user_registration), though I've been unable to do this.
I've found plenty of documentation on custom names for Devise routes, but not for custom actions. Especially not when using devise_for.
To summarize:
I need to disable the default users/registrations#create route
and specify a route to a custom action (users/registrations#custom_one)
with the correct prefix/name (user_registration)
hopefully as elegantly as possible, as I would rather avoid specifying each route independently.
You can achieve this by using the :skip option to devise_for:
devise_for :users, :skip => [:registrations] do
get "/admin" => "devise/registrations#new", :as => :new_user_registration
post "/admin" => "devise/registrations#create", :as => :user_registration
end

Rails Devise user controller in subfolder

I am using omniauth and found devise using a subfolder for this(in official example) controllers/users/omniauth_callbacks_controller.rb. I need to create a User show page as well as other actions for User so I decide to create a new UsersController inside a controllers/users folder. Now it looks like
class Users::UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
end
routes.rb
My::Application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
match 'users/:id' => 'users/users#show'
root :to => 'home#index'
end
it works but the route created is unnamed
rake routes gives
/users/:id(.:format) users/users#show
without GET and route_name
so I'm unable to use it for example after login redirect. Is there a better way to realize the subfolder routes structure and is it good idea to group controllers like this?
You just need name your route in your route.rb
match 'users/:id' => 'users/users#show', :as => 'user'
After that you can call this route by user_url(user.id)
See example on guides : http://guides.rubyonrails.org/routing.html#naming-routes

Resources