fabrik42/acts_as_api

[Ruby 2.0-preview1] NoMethodError (undefined method `empty?' for nil:NilClass)

mrrooijen opened this issue · 4 comments

Switching from MRI 1.9.3 to 2.0 yields the following error:

NoMethodError (undefined method `empty?' for nil:NilClass):
  lib/ext/acts_as_api/responder.rb:9:in `to_html'

This happens in a controller that looks like this:

class SomeController < ApplicationController
  def index
    respond_with SomeModel.all, :api_template => :public
    # respond_with [], :api_template => :public
    # respond_with nil, :api_template => :public
  end
end

Whether you pass in SomeModel.all, [] or nil, all of these yields the same error.

@meskyanichi can you show us the code of lib/ext/acts_as_api/responder.rb ? I run the spec with ruby 2.0 , and did not got the error. and the spec have tested the following action

  def index_no_root_no_order
    @users = User.all
    respond_with @users, :api_template => params[:api_template].to_sym
  end

Sure. Sorry I didn't realize it came from my extension. Here's the code:

# -*- encoding : utf-8 -*-

module App::Ext::ActsAsApi::Responder

  # Override the #to_html method of the ActsAsApi::Responder
  # since we always want to render the layouts/blank when a
  # HTML response needs to be made.
  def to_html
    render "layouts/blank"
  end
end

ActsAsApi::Responder.send(:include, App::Ext::ActsAsApi::Responder)

A little background on this extension. I am overriding the to_html method in the ActsAsApi responder since when a HTML request comes in, I always want to render a blank view (a view without any content) so it renders the html layout. The application itself is a single-page application (using a javascript framework with PushState). When a request comes in at for example http://domain.com/some/path then I basically want Rails to just render the layout without a view, because the layout always contains everything I need, and the view is unnecessary. I wasn't sure how to get that done with ActsAsApi so I simply overwrote the to_html method to render a blank view to get the same result.

Do you know of a way to render the layout but without the action's view? If not, do you have any idea how I could get the desired result in Ruby 2.0 as I am now like this in Ruby 1.9?

Thanks!

Scratch that. Turns out I could simply do this:

def to_html
  render :nothing => true, :layout => true
end

Didn't know you could combine :nothing => true with :layout => true. Works though!

Glad you found the problem, thanks for the follow up!