简体   繁体   中英

JSON REST structure for rails app

I'm building a iOS mobile application with a Rails backend.

This is my first programming endeavor.

At first when I started building the API I created a separate namespace for it and the controllers would respond with "render: json." Now, knowing a little more about routes I realize that I can set a respond_to and respond_with in the controllers thus eliminating the need for any separation. Assuming that at some point there will also be a web application along with other mobile clients is this the best approach? Will this offer more flexibility for programmers who may want to refactor the code later on or vice versa?

I saw this question asked but it offered a solution that separates the API entirely and then utilizes Backbone for the web application front end as the best option. There was no insight into the approach I'm thinking of using.

Generally speaking yes this is the best approach. You will basically create a new set of controllers (typically in a folder called /api/ ) where all of your API controllers using Rails_API will be housed. This means you will potentially have 2 + n controllers to maintain in the future. 1 set of controllers for the web app (standard rails controllers), 1 set of controllers for v1 of your API and "n" controllers for subsequent versions of your API. More information on why APIs need versions here . The problem in the future will be a large maintenance task (depending on the size of your app).

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