0

This looks like an issue to me. Normal way Backbone works is using same URL and GET, POST, PUT and DELETE. But obviously:

1) All but POST methods need an ID either in URL or in request body

2) DELETE request can not contain body or some servers ignore body

So how can you make let's say a Ruby on Rails server app successfully work with Backbone without need to hack Backbone such as model.destroy() needs to have ID in the URL? And, as our RoR developer is telling me, normal way to do routes for PUT is to also have an ID in the URL?

2 Answers 2

2

There are 5 routes you need to implement to make use of backbone's default sync behavior. For a resource user, they are:

GET /user/        // Get a list of users
GET /user/:id     // Get a single users by id
POST /user/       // Create a new user
PUT /user/:id     // Update an existing user by id
DELETE /user/:id  // Delete an existing user by id

I'm not very familiar with Ruby on Rails, but glancing at their documentation you could fulfill this specification with something like:

match "/user/" => "users#all", :via => :get
match "/user/:user_id" => "users#one", :via => :get
match "/user/" => "users#create", :via => :post
match "/user/:user_id" => "users#update", :via => :put
match "/user/:user_id" => "users#delete", :via => :delete
Sign up to request clarification or add additional context in comments.

1 Comment

You should be able to resource :users and get the whole thing: guides.rubyonrails.org/routing.html#crud-verbs-and-actions
1

You should not have to hack Backbone for it to work with RoR. Backbone is smart enough to know (to a certain extent) what URL and what method it should use.

For example, for the initial fetch of a model, if you set url to '/tasks', it will do a GET request to '/tasks/id'. When you change that model, and call model.save, it will do a PUT request to '/tasks/id'. When you call model.destroy, it will send a DELETE request (with an empty body)

The one thing you do have to consider is the CSRF token. I suggest you include backbone-rails in your Gemfile. It includes some JavaScripts to help with Rails/Backbone integration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.