rails resource deep nesting - ruby-on-rails

I have read some articles, and i know, that it's bad to inherit more than 2 level deep resources, but let's forget now about it.
let's imagine, i have such model:
car_brand
car_model
car_type
in route i could write something like this:
namespace :admin do
resources :car_brands do
resources :car_models do
resources :car_types
end
end
end
but i didn't find any good article, how to generate my controller's and view, with such schema,
what i need to write in controller class header, something like: class
Admin::CarBrands::CarModelsController < ApplicationController
or what? I need to clear understand this moment, each sub-model view must be in subfolder view, or how?

Admin::CarTypesController < ApplicationController in controllers/admin folder as car_types_controller.rb
Run rake routes and take a look at this line, for example:
/admin/car_brands/:car_brand_id/car_models/:car_model_id/car_types(.:format)
This is the uri pattern that will map Admin::CarTypesController#index metod. In params hash, you will find :car_brand_id and :car_model_id.

what i need to write in controller class header, something like: class
Admin::CarBrands::CarModelsController < ApplicationController
Partly yes and partly no. Yes, in that you've namespaced routes i.e. within namespace :admin and No, because nested resources do not mean namespaced Controllers as CarBrands::CarModelsController.
Since all resources are within the namespace admin, you'd generate all the controllers as follows:
rails g controller admin/car_brands
rails g controller admin/car_models
rails g controller admin/car_types
Executing each command above would place a controller class and view directory and other test specific files in their corresponding directories. Your question is more towards controllers and views so the directories of concerns are:
- app/controllers/admin/
- app/views/admin/car_brands/
- app/views/admin/car_models/
- app/views/admin/car_types/
Your controller declaration for CarBrandsController would then look like:
class Admin::CarBrandsController < ApplicationController
...
end
With these setup, it's now up to you how you want to manage each controller as a resource. If you nest your car_types within car_models then that the methods in car_models controller will also be expecting car_type_id in parameter. If you don't nest car_types resource then the resource is a standalone resource on it's own and does not have dependencies on any other resources.

Related

How do I know which routes are handled by a controller in Ruby on Rails?

I am trying to write a script/program that given a controller file name, can programmatically determine which routes are served by this controller. This should work for inheritance use cases as well. For example:
class UserController < UserBaseController
end
class UserBaseController
def update
# changes made here
end
end
Running the script given the UserBaseController should return something like: PUT users/:id
How can I go about doing this? I tried using rails routes. However, the routes map to the child class and not the parent class which is where the change was actually made.
Thank you!

Rails - Add a 'GET' sub-route to a main route

Currently I have a route called requests that may have GET/POST endpoints. But another requirement is to achieve the following format: /api/requests/sync.
I tried the following in routes.rb:
Rails.application.routes.draw do
resources :requests do
get "sync"
end
end
But this gives me the following format:
/requests/:request_id/sync
How can I create a sub-route as requests/sync without having it as a sub-route of /:request_id/sync?
Check out the guide. Specifically, collection routes. You'll do something like:
Rails.application.routes.draw do
resources :requests do
collection do
get "sync"
end
end
end
Which will give you requests/sync
To pick up on the sync_controller question...
Personally, not knowing much about what you're actually up to, I would keep sync as an action on the requests_controller. Something like:
class RequestsController < ApplicationController
...
def sync
...
end
...
end
While sync is not one of the standard RESTful actions, it seems more natural to me than creating a new controller. In general, but not always, I think of controllers as being noun-oriented (e.g., 'request', in your case) and actions being verb-oriented. "Sync" seems way more verb-y to me than noun-y.
You could do something along the lines of what Cyzanfar suggests. But, I would suggest you ask yourself:
Do you need all the standard actions for your would-be sync_controller?
Is there some meaningful reason to inherit from Requests::RequestsController?
Do you even need Requests::RequestsControler or could you just do RequestsController and then have Requests::SyncController inherit from RequestsController (which seems less tortured to me)?
And probably other important questions I'm not thinking about on-the-fly.
Here is another way to achieve this with namespacing your controllers so that you can have a distinct controller for sync and requests where the request controller will act as the parent (base) controller.
routes.rb
namespace :requests do
resources :sync
end
requests/requests_controller.rb
class Requests::RequestsController < ApplicationController
end
requests/sync_controller.rb
class Requests::SyncController < Requests::RequestsController
end
Now you'll have the nested CRUD paths under requests
/requests/sync/new
/requests/sync/index
/requests/sync/create
...

Ruby route for custom controller

So I want when I access: site.com/panel to look into /app/controller/panel/index_controller.rb
Before I start I'm new to ruby, I started a couple hours ago
So in my routes.rb I have this
namespace :panel do
root 'index#index'
resources :index
end
And I created a file called index_controller.rb in /app/controller/panel/index_controller.rb which looks like this
class IndexController < ApplicationController
def index
#foo = "Foo"
end
end
Now when I go to site.com/panel I get this: superclass mismatch for class IndexController
What I did wrong?
Also can I setup different views and layout here to use for the controllers inside /app/controller/panel/*_controller.rb
replace this
class IndexController < ApplicationController
with
class Panel::IndexController < ApplicationController
update:
to automatically generate namespaced controller you can use rails build in generator like this
rails g controller panel/users
this will generate Panel::Users < ApplicationController controller under app/controllers/panel/users_controller.rb
Since you've namespaced the index resource routes within panel, you'll need to prefix your IndexController declaration to reflect this:
# app/controllers/index_controller.rb
class Panel::IndexController < ApplicationController
Then, you can similarly reflect the namespace in your filesystem in order to get Rails to properly invoke the correct views:
/app/views/panel/index/index.html.erb
/app/views/panel/index/show.html.erb
... etc
A note: the Rails convention is that routes that are declared as resources should be named plural, as this denotes an entirely resourceful class. Thus, according to this paradigm, index should actually be indexes. However, I suspect you may mean to use a singular route, in which case the declaration would be as follows:
namespace :panel do
resource :index
end
Which creates the following singular routes (which may conform better to what you're trying to accomplish):
panel_index POST /panel/index(.:format) panel/indices#create
new_panel_index GET /panel/index/new(.:format) panel/indices#new
edit_panel_index GET /panel/index/edit(.:format) panel/indices#edit
GET /panel/index(.:format) panel/indices#show
PUT /panel/index(.:format) panel/indices#update
DELETE /panel/index(.:format) panel/indices#destroy

Uninitialized constant "Controller Name"

I'm having an error with my routes/resources and controllers.
I have the following in the routes.rb:
# routes.rb
resources :users do
resource :schedule
end
And I have a schedule_controller.rb inside controllers/users/ set up as I think it should be:
class Users::ScheduleController < ApplicationController
# Controller methods here...
end
Running a rake:routes shows
user_schedule POST /users/:user_id/schedule(.:format) schedules#create
new_user_schedule GET /users/:user_id/schedule/new(.:format) schedules#new
edit_user_schedule GET /users/:user_id/schedule/edit(.:format) schedules#edit
GET /users/:user_id/schedule(.:format) schedules#show
PUT /users/:user_id/schedule(.:format) schedules#update
However, navigating to /users/:user_id/schedule is returning the following error:
uninitialized constant SchedulesController
My only thoughts on what the problem could be are that is has something to do with nested resources or declaring a single resource and I'm going wrong somewhere.
I'm using the helper
new_user_schedule_path(current_user)
when linking to my 'new' view.
It should be SchedulesController, not Users::ScheduleController. Controllers should only be namespaced when the route is namespaced with namespace. Controller names should also always be plural.
What you're creating is a nested resource, not a namespaced one.
Is the namespacing of the SchedulesController intentional? i.e. do you really mean to do this?
class Users::SchedulesController < ApplicationController
Or are you only doing that because schedules are a "sub-thing" from users?
The reason I ask this is because typically within Rails, nested resource controllers aren't namespaced. You would only namespace a controller if you wanted to modify the controllers in a special way under a namespace. A common example of this would be having some controllers under an admin namespace, inheriting from a BaseController within that namespace that would restrict only admins from acessing those controllers.
Option 1
If you didn't intentionally namespace this controller, then you want to remove the Users:: prefix from your controller, and move it back to app/controllers/schedules_controller.rb, the helpers back to app/helpers/schedules_helper.rb and the views back to app/views/schedules. Perhaps you ran a generator which also generated a Users::Schedule model, which should also need to be renamed to Schedule and moved back to app/models/schedule.rb.
Option 2
If you did intentionally namespace this controller, then you want to do this in your routes:
namespace :users do
resources :schedules
end
Leave everything that's been generated as it should be.
In your routes.rb you need to specify the controller like this:
resources :users do
resource :schedules, controller: 'users/schedules'
end
replace resources :users to
namespace :users
Because your schedule controller is inside users folder.
class Users::ScheduleController < ApplicationController
# Controller methods here...
end

Another section in Rails

I saw a lot of discussions about creating another section in Rails 3 but not a complete guide.
I would like to create another section for example
/admin/...
All my previous controllers inherits from
ApplicationController
and use
layout/application.html.erb
So now I want every controller that is places in the newly created /admin/... directory to inherit form a different BaseController and use a different layout than the application.html.erb. If that is possible can you provide a guide about which files has to be created in /admin/... which for layout and what I have to place in the route files??
Thanks in advance.
Create the admin directory under your controllers and then have an 'admin' controller (so they inherit the set layout - also useful for forcing authentication etc), eg
class Admin::AdminController < ApplicationController
layout 'admin/admin'
end
then have your other controllers in the admin directory extend off the admin controller eg
class Admin::CategoriesController < Admin::AdminController
def index
...
end
end
You'll need to create an admin folder under your layouts too and the admin.html.erb (or whatever templating engine you're using, layout can obviously be named whatever you like). Views also for the other admin controller methods will need to live under their respective admin folder, eg app/views/admin/categories/index.html.erb (second admin is the name of the controller
You'll also need to add the routes in your routes.rb - assuming Rails 3
namespace :admin do
root :to => 'admin#index' #default page when accessing /admin
resources :categories #whatever resources you want
...
end
you could add a base_controller.rb in your /admin/ and let your other controllers in /admin/ inherit from Admin::BaseController. Just include a < ApplicationController in your /admin/base_controller.rb.
Now specify the layout in your /admin/base_controller.rb.
For routing you only need to add references available to the generic public. Add a namespace for it:
namespace :admin do
resouces :xyz
end

Resources