ruby - Should I use `respond_to` or have a dedicated API namespace in Rails? -


in rails web app it’s common have actions in controller return json opposed html view.

this common if using many javascript components require json responses.

i writing such action other day , wondered if should move json responses api controller namespace. not provide api future front-end app, rather separate json responses own namespace.

is practice or overengineering?

in experience it's practice separate ui , api controllers, couple of reasons:

  • the ui , api actions 1 needs given model never 1 one. it's confusing have controller in actions respond both ui , api requests , others respond 1 or other.

  • ui , api responses need different support controller: different included modules, different helper methods, different filters , on. it's cleaner have 2 different controllers given model, 1 features needs support ui reponses , features needs support api responses.

in fact, using single controllers serve both ui , api responses great example of how deduplication (of routes , actions) can make code worse.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

json - Gson().fromJson(jsonResult, Myobject.class) return values in 0's -