Reputation: 1642
As per the respond_with/respond_to note in the 4.2 release notes... Ruby Guides and:
I'd like to take this opportunity to split respond_with/class-level respond_to into an external plugin. I'm generally not a fan of the code that comes out of using this pattern. It encourages model#to_json and it hides the difference between HTML and API responses in ways that convolute the code.
So how about we split this into a gem for 4.2, with the current behavior, but also with an option to get the new behavior as suggested here through a configuration point. - DHH
I have installed ActiveModelSerializer 0.9.3 and I have found out that it still works with:
render json: @object
in the controller...
How can I render json:
by default in Application controller?
Upvotes: 3
Views: 2534
Reputation: 591
You can modify your routes.rb
files to specify the default format
routes.rb
resources :clients, defaults: {format: :json}
This will modify the default response format for your entire clients_controller
Upvotes: 2
Reputation: 608
You should not need to render any view directly from application controller but from a controller extending ApplicationController. If you say respond_with is still working in your rails version then you just need to put respond_to :json such as:
class MyController < ApplicationController
respond_to :json
def my_action
....
respond_with(@variable)
end
end
respond_with will automatically render the view in correct format based on the mime type of your request from there.
If it's not working then add gem 'responders', '~> 2.0' to your gem file.
Upvotes: 3