whoaaallamapajama
whoaaallamapajama

Reputation: 101

Restful resources rails

What is the correct way to make this api 'restful'

I have a location,

GET location/:id
PUT location/:id

The spec has changed and now this location can have certain 'types' the client can update. One requirement is that the client get the allowed types (could be one of these from a fixed list - i.e.['urban', 'wilderness', 'private', etc..]) for all locations. This isn't a nested resource, its a column on the location table. I've implemented,

GET location/types_allowed

is there are better RESTFUL way?

Another example, a location can have neighbors, so I made GET locations/neighbors. I'm having trouble understanding the nuances of REST beyond CRUD and nested resources. Thanks!

Upvotes: 0

Views: 118

Answers (2)

Taryn East
Taryn East

Reputation: 27747

I strongly recommend against naming a model/column "type". That tends to break magic things inside of rails that you don't even know are there until you try that. It's one of those "undocumented features" :P

I'd instead recommend reading up on "single table inheritance" or "polymorphism" to figure out if either of these is right for you.

If not... then name something "kind" instead of "type" as it'll all work a lot better that way.

As to RESTful resources... you really can't go past the Rails Guides. If you follow the routing guide: (and all the other ones) you'll get an idea of how to do RESTful resources

Upvotes: 1

Subash
Subash

Reputation: 3168

Instead of a column as types_allowed, create a type resource and the relation can be like

location.rb
  belongs_to :type

type.rb
  has_many :locations

and with this your route file would be

resources :type do
  resources :location
end

Upvotes: 1

Related Questions