简体   繁体   中英

When do we need to create a new controller?

Currently for my GET REST service I have a controller that goes and grabs the JSON from six tables that I need. For example:

class MySampleController < ApplicationController

respond_to :json

def show
  @organization = Organization.includes([:ThoseSixTables])
  respond_with(@organization)
end

and then in my route I will have something like:

resources :my_sample, only: [:show]

So when the client calls my service in a url like /my_sample/1.json it will return the JSON to it.

The web-page I am writing these services for, has a couple of more navigation links on it like tabs, so now if they click on a VicePresident link we should call a new GET REST service and return a whole new set of Data to them.

So do I need to create a new controller for it? or I can use the same controller and add a nested resource in the routes? For example to be able to get the new JSON from a URI like /my_sample/1/vpview

Here is some good reading for you. http://guides.rubyonrails.org/routing.html

I would make a new controller for every resource. Also be careful about nesting resources you really dont want to get too deep.

It is really up to you to design the urls you like, and see any pattern that can be grouped together (like lots of people profile page will have the same structure json file).

For me, I am guessing your app display only one organization, not multiple of them. This probably means it is a one-off page. I usually just put these one-off pages as methods in an abouts-controller.

Your vice-president page sounds like a people resource, meaning you may in the future add another page for CEO, etc. Therefore it feels natural to have a people controller. This allows future re-usability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM