rails 3 response format and versioning using vendor MIME type in the Accept header

后端 未结 2 1874
难免孤独
难免孤独 2021-02-04 00:11

Preamble:

I investigated how to version an API and found several ways to do it. I decided to try peter williams\' suggestion and created new vendor mi

2条回答
  •  温柔的废话
    2021-02-04 00:31

    I haven't seen this content type trick used anywhere in a Rails project before so this is new to me. The way I've typically seen it done is to define a route namespace (e.g. /api/v1/) which goes to a controller (say, Api::Version1Controller).

    Also, I know you want to do things the "Rails way", and maybe this sounds crotchety coming from a guy who has been with Rails since 1.3, but the whole respond_with / respond_to stuff is rather magic to me. I didn't know that respond_to looks for a to_XXX method when it serializes objects, for instance (maybe I need to read up on that). Having to monkey-patch Array like that seems rather silly. Besides, for an API, formatting the model data is really the view's job, not the model's. I might look into something like rabl in this case. There's a good writeup about it here.

提交回复
热议问题