Routing error for RESTful API - Rails

I am creating an API for a web application that I have been working on for a while. I started with the User model. The user-side API will allow remote clients to a) retrieve user data, b) update user information, and c) create new users. I got it all to work, but it doesn't seem to be setting it up correctly. Here are my questions:

  • Should the API endpoint be user or user? What's the best practice?
  • I need to add an action to the end and I expect the request type to be selected instead, so I don't have to explicitly specify it. How do I properly configure my routes to not include a protected action method?

Let me give you a few examples:

Get a request to show - you want it to work without "show"

Put an update request - you want it to work without "updating"

Create - Works with or without creating what I want for all these actions.

How to structure routes so that you don't require an action name? Isn't this the usual way to use RESTful APIs?

Here is my route for the API now:

map.namespace :api do |route|
  route.resources :users
  route.resources :weight
end

      

I am using calm authentication which handles HTTP authentication in curl. Any guidance on routes and best practice regarding singular and plural would be really helpful.

Thanks! -A

+2


a source to share


1 answer


  • Singular and multiple endpoints for residual APIs depend on the model you are returning. Your URL should state that there can be multiple instances of the user resource.

  • Your routes are configured correctly. GET and PUT requests must include the user ID at the endpoint, i.e.

    curl -u rmbruno: blah http: //app.local/api/users/1



See this answer for an overview of why rails create these "friendly" URLs for CRUD actions: Rails - Redundant RESTful Actions for map.resources? (new, create)

+1


a source







All Articles