roar-rails icon indicating copy to clipboard operation
roar-rails copied to clipboard

API Versioning and Roar

Open chadwtaylor opened this issue 10 years ago • 60 comments

Everything worked great for a model. When it came to an array/collection of models, this is where I'm stuck and I wonder if it has to do with the fact my Rails API project is versioned (ie: v1, v2)?

The error message I'm getting when trying to load a collection:

uninitialized constant Api::V2::PeopleRepresenter (and I already instructed the render to use represent_items_with: Api::V2::PersonRepresenter). I wonder if it has to do with modules/namespacing things?

Here's what my set up looks like...

app/api/v2/api_controller

require 'roar'

module V2
  class Api::V2::ApiController < ActionController::API

    include Roar::Rails::ControllerAdditions
    include Roar::Rails::ControllerAdditions::Render

    respond_to :json
    ...
  end
end

app/api/v2/people_controller

class Api::V2::PeopleController < Api::V2::ApiController

  # GET /people
  def index
    people = Person.find([8,18])
    render json: people, represent_items_with: Api::V2::PersonRepresenter
  end

  ...

end

app/representers/api/v2/person_representer

module Api
  module V2
    class PersonRepresenter < Roar::Decorator
      include Roar::JSON::JSONAPI

      type :people

      property :id
      property :first_name
      property :last_name
      property :full_name

    end
  end
end

chadwtaylor avatar Feb 02 '15 23:02 chadwtaylor

Have you looked into the ::represents class method, yet? https://github.com/apotonick/roar-rails#represents-configuration

apotonick avatar Feb 02 '15 23:02 apotonick

Since each method will vary in which model will be rendered (ie: most of the time a person model, rarely will be an invoice model). So putting it on the class level may not be favorable in my situation? I gave it a try though and got another error: undefined method 'id' for #<Array:0x007fde1099e670>

I'm hoping I can create one PersonRepresenter that will deal with collections as well, hence the represent_items_with at the method level.

Thanks for your invaluable help!

chadwtaylor avatar Feb 02 '15 23:02 chadwtaylor

I'm still wondering, though, why the :represent_items_with option doesn't kick in?

apotonick avatar Feb 03 '15 00:02 apotonick

What else do you need to see from my end to narrow down the issue?

chadwtaylor avatar Feb 03 '15 00:02 chadwtaylor

I even tried to change from render json: people, represent_items_with:Api::V2::PersonRepresenter to respond_with people, represent_items_with:Api::V2::PersonRepresenter and got the following error:

ActionController::UnknownFormat

Am hitting a wall here but going to try few more things and hopefully get the collections to work (emphasis: The single model worked nicely).

chadwtaylor avatar Feb 03 '15 01:02 chadwtaylor

Ah, I got it! The :represent_items_with option doesn't work with render! If you use respond_with you gotta call respond_to on the controller level (that's Rails, not roar-rails!)!

apotonick avatar Feb 03 '15 01:02 apotonick

I switched to respond_with with the respond_to on the controller level (see my codes below) -- and got this error:

ActionController::UnknownFormat

The only thing working for me is using render (on a single model). Any ideas?

# app/api/v2/people_controller.rb

class Api::V2::PeopleController < Api::V2::ApiController
  include Roar::Rails::ControllerAdditions
  respond_to :json

  def index
    p = Person.first
    respond_with p #=> This resulted in ActionController::UnknownFormat
  end

end


# app/representers/api/v2/person_representer.rb

module Api
  module V2
    module PersonRepresenter 
      include Roar::JSON::JSONAPI

      type :people

      property :id
      property :first_name
      property :last_name
      property :full_name

    end
  end
end

chadwtaylor avatar Feb 03 '15 14:02 chadwtaylor

Can you paste the server output or the test line for that failling request? It must be something in Rails, we don't throw UnknownFormat anywhere! Thanks!

apotonick avatar Feb 03 '15 21:02 apotonick

Started GET "/v2/people" for 127.0.0.1 at 2015-02-03 14:15:35 -0800
Processing by Api::V2::PeopleController#index as HTML
  Person Load (0.5ms)  SELECT  "people".* FROM "people"  ORDER BY "people"."id" ASC LIMIT 1
Completed 406 Not Acceptable in 3ms

ActionController::UnknownFormat - ActionController::UnknownFormat:
  responders (2.1.0) lib/action_controller/respond_with.rb:205:in `respond_with'
  app/controllers/api/v2/people_controller.rb:12:in `index'
  actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/rack/agent_hooks.rb:26:in `traced_call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/rack/browser_monitoring.rb:23:in `traced_call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/rack/developer_mode.rb:56:in `traced_call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  rack (1.6.0) lib/rack/etag.rb:24:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.6.0) lib/rack/conditionalget.rb:25:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.6.0) lib/rack/head.rb:13:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  appsignal (0.11.0) lib/appsignal/rack/listener.rb:13:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  airbrake (4.1.0) lib/airbrake/rails/middleware.rb:13:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:57:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.6.0) lib/rack/runtime.rb:18:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rocket_pants (1.10.0) lib/rocket_pants/cache_middleware.rb:21:in `_call'
  rocket_pants (1.10.0) lib/rocket_pants/cache_middleware.rb:12:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.6.0) lib/rack/lock.rb:17:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack-cors (0.3.1) lib/rack/cors.rb:72:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  airbrake (4.1.0) lib/airbrake/user_informer.rb:16:in `_call'
  airbrake (4.1.0) lib/airbrake/user_informer.rb:12:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  hirefire-resource (0.3.4) lib/hirefire/middleware.rb:29:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  railties (4.2.0) lib/rails/engine.rb:518:in `call'
  railties (4.2.0) lib/rails/application.rb:164:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.6.0) lib/rack/content_length.rb:15:in `call'
  puma (2.9.2) lib/puma/server.rb:490:in `handle_request'
  puma (2.9.2) lib/puma/server.rb:361:in `process_client'
  puma (2.9.2) lib/puma/server.rb:254:in `block in run'
  puma (2.9.2) lib/puma/thread_pool.rb:92:in `block in spawn_thread'

chadwtaylor avatar Feb 03 '15 22:02 chadwtaylor

(want to say how much I appreciate your help here!)

chadwtaylor avatar Feb 03 '15 22:02 chadwtaylor

"No worries, mate!" as they say in Australia! :wink:

This is your problem: Processing by Api::V2::PeopleController#index as HTML

Your request is not a JSON request! That's why Rails complains, it doesn't know how to handle a generic HTML request.

apotonick avatar Feb 03 '15 22:02 apotonick

Ah, so I appended .json to the url like so: http://localhost:3001/v2/people.json and a new error came up:

Started GET "/v2/people.json" for 127.0.0.1 at 2015-02-03 14:25:17 -0800
Processing by Api::V2::PeopleController#index as JSON
  Person Load (0.3ms)  SELECT  "people".* FROM "people"  ORDER BY "people"."id" ASC LIMIT 1
Completed 500 Internal Server Error in 18ms

NoMethodError - undefined method `default_render' for #<Api::V2::PeopleController:0x007fdf3bb770d8>:
  responders (2.1.0) lib/action_controller/responder.rb:236:in `default_render'
  responders (2.1.0) lib/action_controller/responder.rb:186:in `to_format'
  responders (2.1.0) lib/action_controller/responder.rb:163:in `respond'
  responders (2.1.0) lib/action_controller/responder.rb:156:in `call'
  responders (2.1.0) lib/action_controller/respond_with.rb:203:in `respond_with'
  app/controllers/api/v2/people_controller.rb:12:in `index'
  actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/rack/agent_hooks.rb:26:in `traced_call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/rack/browser_monitoring.rb:23:in `traced_call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/rack/developer_mode.rb:56:in `traced_call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  rack (1.6.0) lib/rack/etag.rb:24:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.6.0) lib/rack/conditionalget.rb:25:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.6.0) lib/rack/head.rb:13:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  appsignal (0.11.0) lib/appsignal/rack/listener.rb:13:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  airbrake (4.1.0) lib/airbrake/rails/middleware.rb:13:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:57:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.6.0) lib/rack/runtime.rb:18:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rocket_pants (1.10.0) lib/rocket_pants/cache_middleware.rb:21:in `_call'
  rocket_pants (1.10.0) lib/rocket_pants/cache_middleware.rb:12:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.6.0) lib/rack/lock.rb:17:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack-cors (0.3.1) lib/rack/cors.rb:72:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  airbrake (4.1.0) lib/airbrake/user_informer.rb:16:in `_call'
  airbrake (4.1.0) lib/airbrake/user_informer.rb:12:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  hirefire-resource (0.3.4) lib/hirefire/middleware.rb:29:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  railties (4.2.0) lib/rails/engine.rb:518:in `call'
  railties (4.2.0) lib/rails/application.rb:164:in `call'
  newrelic_rpm (3.9.7.266) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.6.0) lib/rack/content_length.rb:15:in `call'
  puma (2.9.2) lib/puma/server.rb:490:in `handle_request'
  puma (2.9.2) lib/puma/server.rb:361:in `process_client'
  puma (2.9.2) lib/puma/server.rb:254:in `block in run'
  puma (2.9.2) lib/puma/thread_pool.rb:92:in `block in spawn_thread'

chadwtaylor avatar Feb 03 '15 22:02 chadwtaylor

I hate Rails.

apotonick avatar Feb 03 '15 22:02 apotonick

What roar-rails version are you on?

apotonick avatar Feb 03 '15 22:02 apotonick

roar 1.0.0 roar-rails 1.0.1 rails 4.2.0

I love Rails. :)

chadwtaylor avatar Feb 03 '15 22:02 chadwtaylor

That's because you haven't seen how simple things can be with less Rails: https://leanpub.com/trailblazer

apotonick avatar Feb 03 '15 22:02 apotonick

I'm pretty sure -- the thing is we're knee-deep into a Rails project... so we can't switch boats just yet. :(

chadwtaylor avatar Feb 03 '15 23:02 chadwtaylor

I hear ya! :stuck_out_tongue_winking_eye:

Trailblazer is Rails, just less coupling to the actual framework and less weirdness going on in controllers and models.

I really have no idea what's going on on your side - I checked an tests pass with Rails 4.2 and roar-rails. Are you using rails-api?

apotonick avatar Feb 03 '15 23:02 apotonick

AH, I know what's your problem.

You got a file app/views/v2/peoples/index.html.haml and that makes it try to call render_default and this is exactly why I hate Rails.

Explanation: Rails and its responders use exceptions to find out whether or not a template is present and then base further behavior on that. And that is just wrong as it is completely hidden semantic, but, hey, magic, yeah, FTW! :stuck_out_tongue:

apotonick avatar Feb 03 '15 23:02 apotonick

Correct, am using rails-api and the file index.html.haml doesn't even exist... this is so weird. Yea, FTW, will continue to play around and get something going! :)

Thanks!

chadwtaylor avatar Feb 04 '15 00:02 chadwtaylor

Are there any files in your app/views? See, this is where the wrong flow starts: https://github.com/plataformatec/responders/blob/master/lib/action_controller/responder.rb#L190

Wait, what was your actual problem in the first place? We can make that work and you keep using render ?

apotonick avatar Feb 04 '15 00:02 apotonick

Yes, I have views but they're for mailers only (ie: app/view/person_mailer).

My issue was trying to keep one representer for each models (rather than creating two for each model like PersonRepresenter and PeopleRepresenter).

I even created a PeopleRepresenter and used render, it found the PeopleRepresenter file but I got this error: undefined method 'id' for #<Array:0x007fe3277c3ac8>

I wonder though if the people_representer.rb is supposed to have a certain implementation? I basically copied-pasted-renamed from person_representer.rb to people_representer.rb.

chadwtaylor avatar Feb 04 '15 00:02 chadwtaylor

It sounds like your representers for singular models are ok. However, you need to define a representer for collections, too (or use represent_items_with, which isn't implemented for render, YET cough wink).

apotonick avatar Feb 04 '15 00:02 apotonick

Do you have a sample of what a representer for collections should look like?

Yea, represent_items_with for render would be awesome… one day! :)

chadwtaylor avatar Feb 04 '15 00:02 chadwtaylor

Tons of examples in issues, check Lonely Collections. Or buy my book and wait a few weeks until we get to the document API parts. _cough_ _COUGH_*

apotonick avatar Feb 04 '15 01:02 apotonick

:)

Ah, thanks for the Lonely Collection tip -- I overlooked that!

Now things are rendering for collections high five and there's a but... Here's what the result looked like:

[
  {
    "people":{
      "id":8,
      "first_name":"Jane",
      "last_name":"Doe",
      "full_name":"Jane Doe",
    }
  },
  {
    "people":{
      "id":18,
      "first_name":"John",
      "last_name":"Doe",
      "full_name":"John Doe",
    }
  }
]

I would think it should look like this...

{
  people: [
    {
      id: 8,
      first_name: "Jane",
      last_name: "Doe",
      full_name: "Jane Doe",
    },
    {
      id: 18,
      first_name: "John",
      last_name: "Doe",
      full_name: "John Doe",
    }
  ] 
}

Here's what my PeopleRepresenter looks like:

module Api
  module V2
    class PeopleRepresenter < Roar::Decorator
      include Representable::JSON::Collection
      items extend:Api::V2::PersonRepresenter, class:Person
    end
  end
end

chadwtaylor avatar Feb 04 '15 04:02 chadwtaylor

Your PersonRepresenter must have a representation_wrap set?

apotonick avatar Feb 04 '15 05:02 apotonick

Ah, thanks... just added self.represenation_wrap = :people and we are so close... Here's the new JSON payload:

{
  people: [
    {
      "people":{
        "id":8,
        "first_name":"Jane",
        "last_name":"Doe",
        "full_name":"Jane Doe",
      }
    },
    {
      "people":{
        "id":18,
        "first_name":"John",
        "last_name":"Doe",
        "full_name":"John Doe",
      }
    }
  ]
}

chadwtaylor avatar Feb 04 '15 05:02 chadwtaylor

Haha, no, I am saying Api::V2::PersonRepresenter should NOT have a representation wrap set, otherwise it will add it for every item. Paste Api::V2::PersonRepresenter.

apotonick avatar Feb 04 '15 05:02 apotonick

(will paste soon; BTW just bought your book....)

chadwtaylor avatar Feb 04 '15 05:02 chadwtaylor