Jimmy
Jimmy

Reputation: 37141

Using different logic for the same respond_to format in Rails

My Rails application is currently using JSON as the respond_to format for AJAX forms on the site. I'm planning to create a public API for the application, and I'd like to use JSON for it also. How might I distinguish between an AJAX form and an API call in my controllers if the requested format for both is JSON?

Upvotes: 0

Views: 283

Answers (1)

Jeremy
Jeremy

Reputation: 4930

I'd recommend you keep the public api in a separate controller, and have a different route since you say you have different logic. You can have namespaced controllers such as app/controllers/api/users_controller.rb and namespace your routes like:

namespace :admin do
  resources :users
end

and leave your existing controllers with ajax actions used by your site as they are.

Upvotes: 2

Related Questions