Using ROAR ( https://github.com/apotonick/roar ) and without using ActiveRecord or any view-templates I want to:
config/initializers/mime_types.rb:
Mime::Type.register "application/vnd.xxx-v1+json" , :xxx_v1
controllers/api/base_controller.rb
class Api::V1::BaseController < ActionController::Base
respond_to :xxx_v1
ActionController.add_renderer :xxx_v1 do |obj, options|
json = obj.to_json
self.content_type ||= Mime::Type.lookup('application/vnd.xxx-v1+json')
self.response_body = json
end
end
controllers/api/user_controller.rb:
class Api::V1::UsersController < Api::V1::BaseController
def index
respond_with User.all
end
end
GET http://domain/users Accept application/vnd.xxx-v1
ERROR: Missing template api/v1/users/index, api/v1/base/index with {:handlers=>[:erb, :builder], :formats=>[:xxx_v1], :locale=>[:en, :en]}
After reading several posts like Rails Custom Renderer or rails 3 response format and versioning using vendor MIME type in the Accept header I still can't get it work..
-=== small update ===-
When I use render directly in the controller, it's working! Always problems with respond_with...
render :xxx_v1 => User.all
Solution for rendering:
Put an empty method into your models:
def to_xxx_v1
end
It is not called but has to be present for respond_with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With