ccocchi / rabl-rails

Rails 4.2+ templating system with JSON, XML and Plist support.
MIT License
209 stars 51 forks source link

Rails 4.1 #57

Closed ethicalhack3r closed 10 years ago

ethicalhack3r commented 10 years ago

Has anyone tried running rabl-rails 0.9.3 with Rails 4.1?

Since upgrading to Rails 4.1 any specs that have RABL templates are failing with the following error:

undefined method each' for nil:NilClass

Full Stack Trace:

rack (1.5.2) lib/rack/etag.rb:58:in `digest_body'
rack (1.5.2) lib/rack/etag.rb:26:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.0) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.0) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.0) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.0) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.0) lib/rails/engine.rb:514:in `call'
railties (4.1.0) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/ryan/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/ryan/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/ryan/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

My hunch is that this error is due to rabl-rails not supporting Rails 4.1 yet? I could be wrong and the error might be related to something else, thought I'd see if anyone else here had encountered this error with rabl-rails and Rails 4.1.

ccocchi commented 10 years ago

I haven't really tried the gem against Rails 4.1 but it seems they have changed some internals rendering. Can you provide the template and the controller as well so I can look into it?

Thank you for reporting this.

ethicalhack3r commented 10 years ago

My API code is based on this tutorial found here - http://railsware.com/blog/2013/04/08/api-with-ruby-on-rails-useful-tricks/

Here is my base controller:

# http://railsware.com/blog/2013/04/08/api-with-ruby-on-rails-useful-tricks/

class Api::V1::BaseController < ActionController::Metal
  include ActionController::Rendering        # enables rendering
  include ActionController::MimeResponds     # enables serving different content types like :xml or :json
  include AbstractController::Callbacks      # callbacks for your authentication logic
  include ActionController::ImplicitRender   # https://github.com/rails-api/rails-api/issues/93
  include AbstractController::Rendering      # for append_view_path Rails > 4.1

  append_view_path "#{Rails.root}/app/views" # you have to specify your views location as well

  before_filter :set_content_type, :set_cors_headers

  private

  def set_content_type
    headers['Content-Type'] = 'application/json'
  end

  def set_cors_headers
    headers['Access-Control-Allow-Origin']   = '*'
    headers['Access-Control-Allow-Methods']  = 'GET'
    headers['Access-Control-Request-Method'] = 'GET'
    headers['Access-Control-Allow-Headers']  = 'Origin, X-Requested-With, Content-Type, Accept'
  end
end

An API end-point controller which uses the base controller above (also tested without the friendly_id gem):

class Api::V1::PluginsController < Api::V1::BaseController
  def show
    @plugin = Plugin.friendly.find(params[:id])
  end
end

And the view (show.rabl):

object @plugin

attribute :name => :name
attribute :public_id => :id
attributes :title, :created_at, :source

child :vulnerabilities do
  attributes :id, :title
  attribute :osvdb, :unless => lambda { |m| m.osvdb.blank? }
  attribute :secunia, :unless => lambda { |m| m.secunia.blank? }
  attribute :cve, :unless => lambda { |m| m.cve.blank? }
  attribute :references, :unless => lambda { |m| m.references.blank? }
  attribute :fixed_in, :unless => lambda { |m| m.fixed_in.blank? }
end
ethicalhack3r commented 10 years ago

It seems that if I change ActionController::Metal to ActionController::Base in the base controller, my specs pass without error. Doesn't seem to be caused by this gem, instead looks like I may need to include another AbstractController and/or ActionController class which ActionController::Metal doesn't include anymore in version 4.1.

alankrit-zz commented 9 years ago

@ethicalhack3r : Did you figure out what needs to be included in the ActionController::Metal?

I am facing the same issue. Followed the same tutorial.

I am building my app on Rails 4.1.6

ethicalhack3r commented 9 years ago

@alankrit in the end I ended up including so many components I just ended up going back to using ActionController::Base

So my first line of my controller looks like:

class Api::V1::BaseController < ActionController::Base