i didn't find anything for my special question. There exist some related question but nothing for this kind of problem:
Rails 4 match all routes for one controller
One controller for multiple routes
I have the following user model:
class User < ApplicationRecord
has_many :customers, foreign_key: :user_id, class_name: 'User'
has_many :users
end
A user can have customers and also have users. And each of the users can have customers (and if needed also users which allready can have customers and so one)
Now i want to build one controller for each type of users.
class UsersController < ApplicationController
def index
@users = User.type_for(params[:type], current_user)
end
end
The routes.rb is also clear:
resources :users
But i don't want the following routes:
For Users: /users?type=user
For Customers: /users?type=customer
It will be nicer to have the following:
/users => UserController#index (params[:type] = user
/customers => UserController#index (params[:type] = customer
Like a
resources :users, class_name: 'User', type: :user
resources :customers, class_name: 'User', type: :customer
I just found one similar
resources :users, :collection => { :customers => :get }
but for this i have to create 2 different methods (one for customers, one for users)