trailblazer / roar-rails

Use Roar's representers in Rails.
http://roar.apotomo.de
MIT License
235 stars 65 forks source link

Uninitialized constant Mongoid for index action #51

Closed monfresh closed 9 years ago

monfresh commented 11 years ago

Hi,

I'm trying roar-rails for the first time (Rails 3.2.13, Mongoid 3.1.4), and I was able to get the show action to work just fine, but the index action throws an Uninitialized constant Mongoid error. What am I doing wrong?

Here is app/representers/programs_representer.rb:

require 'representable/json/collection'

module ProgramsRepresenter
  include Representable::JSON::Collection
  items extend: ProgramRepresenter, class: Program
end

Here is programs_controller.rb:

module Api
  module V1
    class ProgramsController < ApplicationController

      include Roar::Rails::ControllerAdditions
      respond_to :json

      def index
        programs = Program.all
        respond_with programs
      end

      def show
        program = Program.find(params[:id])
        respond_with program
      end

    end
  end
end

Here is program_representer.rb, which works with the show action:

require 'roar/representer/json'

module ProgramRepresenter
  include Roar::Representer::JSON

  property :id
  property :name
  property :description
end
apotonick commented 11 years ago

Can you paste the exception stacktrace? We've had some problems with mongoid a while ago, thou...

monfresh commented 11 years ago

Here you go. Thanks for looking into it.

Started GET "/api/programs/" for 127.0.0.1 at 2013-08-20 22:06:30 -0400
Processing by Api::V1::ProgramsController#index as JSON
Completed 500 Internal Server Error in 8ms

NameError - uninitialized constant Api::V1::Mongoid:
  activesupport (3.2.13) lib/active_support/inflector/methods.rb:230:in `block in constantize'
  activesupport (3.2.13) lib/active_support/inflector/methods.rb:229:in `each'
  activesupport (3.2.13) lib/active_support/inflector/methods.rb:229:in `constantize'
  activesupport (3.2.13) lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
  roar-rails (0.1.3) lib/roar/rails/controller_additions.rb:121:in `infer_representer'
  roar-rails (0.1.3) lib/roar/rails/controller_additions.rb:117:in `entity_representer'
  roar-rails (0.1.3) lib/roar/rails/controller_additions.rb:102:in `name_for'
  roar-rails (0.1.3) lib/roar/rails/controller_additions.rb:84:in `for'
  roar-rails (0.1.3) lib/roar/rails/controller_additions.rb:42:in `representer_for'
  roar-rails (0.1.3) lib/roar/rails/controller_additions.rb:36:in `prepare_model_for'
  roar-rails (0.1.3) lib/roar/rails/responder.rb:54:in `prepare_model_for'
  roar-rails (0.1.3) lib/roar/rails/responder.rb:7:in `display'
  actionpack (3.2.13) lib/action_controller/metal/responder.rb:202:in `api_behavior'
  actionpack (3.2.13) lib/action_controller/metal/responder.rb:181:in `rescue in to_format'
  actionpack (3.2.13) lib/action_controller/metal/responder.rb:175:in `to_format'
  actionpack (3.2.13) lib/action_controller/metal/responder.rb:153:in `respond'
  actionpack (3.2.13) lib/action_controller/metal/responder.rb:146:in `call'
  actionpack (3.2.13) lib/action_controller/metal/mime_responds.rb:239:in `respond_with'
  app/controllers/api/v1/programs_controller.rb:17:in `index'
  actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  activesupport (3.2.13) lib/active_support/callbacks.rb:414:in `_run__4391624702961111214__process_action__3657460046371578378__callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
  activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
  newrelic_rpm (3.6.5.130) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:38:in `block in process_action'
  newrelic_rpm (3.6.5.130) lib/new_relic/agent/instrumentation/controller_instrumentation.rb:318:in `perform_action_with_newrelic_trace'
  newrelic_rpm (3.6.5.130) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:37:in `process_action'
  actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
  actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
  actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
  actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
  actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
  actionpack (3.2.13) lib/action_dispatch/routing/mapper.rb:42:in `call'
  journey (1.0.4) lib/journey/router.rb:68:in `block in call'
  journey (1.0.4) lib/journey/router.rb:56:in `each'
  journey (1.0.4) lib/journey/router.rb:56:in `call'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
  rack-pjax (0.7.0) lib/rack/pjax.rb:12:in `call'
  newrelic_rpm (3.6.5.130) lib/new_relic/rack/error_collector.rb:12:in `call'
  newrelic_rpm (3.6.5.130) lib/new_relic/rack/agent_hooks.rb:22:in `call'
  newrelic_rpm (3.6.5.130) lib/new_relic/rack/browser_monitoring.rb:16:in `call'
  newrelic_rpm (3.6.5.130) lib/new_relic/rack/developer_mode.rb:28:in `call'
  bullet (4.6.0) lib/bullet/rack.rb:13:in `call'
  mongoid (3.1.4) lib/rack/mongoid/middleware/identity_map.rb:34:in `block in call'
  mongoid (3.1.4) lib/mongoid/unit_of_work.rb:39:in `unit_of_work'
  mongoid (3.1.4) lib/rack/mongoid/middleware/identity_map.rb:34:in `call'
  rack-cors (0.2.8) lib/rack/cors.rb:54:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `catch'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  rack (1.4.5) lib/rack/etag.rb:23:in `call'
  rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
  remotipart (1.2.1) lib/remotipart/middleware.rb:27:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
  rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
  rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__2844715905734404138__call__115742238314815718__callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
  quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
  actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  lib/api_defender.rb:47:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
  rack-timeout (0.0.4) lib/rack/timeout.rb:16:in `block in call'
  /Users/moncef/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/timeout.rb:66:in `timeout'
  rack-timeout (0.0.4) lib/rack/timeout.rb:16:in `call'
  railties (3.2.13) lib/rails/engine.rb:479:in `call'
  railties (3.2.13) lib/rails/application.rb:223:in `call'
  railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'
  rack (1.4.5) lib/rack/lint.rb:48:in `_call'
  rack (1.4.5) lib/rack/lint.rb:36:in `call'
  rack (1.4.5) lib/rack/showexceptions.rb:24:in `call'
  rack (1.4.5) lib/rack/commonlogger.rb:33:in `call'
  rack (1.4.5) lib/rack/chunked.rb:43:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  unicorn (4.6.3) lib/unicorn/http_server.rb:552:in `process_client'
  unicorn (4.6.3) lib/unicorn/http_server.rb:632:in `worker_loop'
  unicorn (4.6.3) lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
  unicorn (4.6.3) lib/unicorn/http_server.rb:142:in `start'
  unicorn (4.6.3) bin/unicorn:126:in `<top (required)>'
   () Users/moncef/.rvm/gems/ruby-2.0.0-p247@ohana-api/bin/unicorn:23:in `load'
   () Users/moncef/.rvm/gems/ruby-2.0.0-p247@ohana-api/bin/unicorn:23:in `<main>'
   () Users/moncef/.rvm/gems/ruby-2.0.0-p247@ohana-api/bin/ruby_noexec_wrapper:14:in `eval'
   () Users/moncef/.rvm/gems/ruby-2.0.0-p247@ohana-api/bin/ruby_noexec_wrapper:14:in `<main>'
apotonick commented 11 years ago

Can you paste the Program class as well, please? Thanks!

monfresh commented 11 years ago
class Program
  include Mongoid::Document
  include Mongoid::Timestamps

  belongs_to :organization
  validates_presence_of :organization

  has_many :locations, dependent: :destroy

  embeds_many :contacts
  accepts_nested_attributes_for :contacts

  normalize_attributes :audience, :description, :eligibility, :fees,
    :how_to_apply, :name, :short_desc, :urls

  field :audience
  field :description
  field :eligibility
  field :fees
  field :how_to_apply
  field :name
  field :short_desc

  field :funding_sources, type: Array
  field :urls, type: Array

  validates_presence_of :name, :description

  extend ValidatesFormattingOf::ModelAdditions

  validates :urls, array: {
    format: { with: %r{\Ahttps?://([^\s:@]+:[^\s:@]*@)?[A-Za-z\d\-]+(\.[A-Za-z\d\-]+)+\.?(:\d{1,5})?([\/?]\S*)?\z}i,
              message: "Please enter a valid URL" } }

end
apotonick commented 11 years ago

It would be awesome if you could add a test here: https://github.com/apotonick/roar-rails/blob/master/test/responder_test.rb#L338

Just include mongoid as a development dependency, set up a simple model and try to render it using the test style found in the file above.

monfresh commented 11 years ago

When I reported this issue, I was trying to use Roar with RocketPants, but I've since switched to Grape, and everything is working fine. I still have the branch with RocketPants, so I'll try to reproduce the issue and write a test.

apotonick commented 11 years ago

Thanks, that would be greatly appreciated.