Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix render_views for anonymous controllers on 1.8.7. #525

Closed
wants to merge 1 commit into from

2 participants

@mudge

Rails uses a controller's class name to determine the layout to use when
rendering views but it does so with an inherited hook. This means that
setting the name of a class after it inherits is too late. This is not
an issue in Ruby 1.9.2 as the name of an anonymous module is set to nil
but in 1.8.7 it is set to the empty string (which, of course, is a
truthy value). This causes Rails to throw a "can't convert nil into
String" error when attempting to render views from an anonymous
controller.

We can fix this by declaring an anonymous controller's name on
creation
and then evaluating the body of the controller with
class_eval.

This fixes issue #450.

James Hunt & Paul Mucur Fix render_views for anonymous controllers on 1.8.7.
Rails uses a controller's class name to determine the layout to use when
rendering views but it does so with an inherited hook. This means that
setting the name of a class *after* it inherits is too late. This is not
an issue in Ruby 1.9.2 as the name of an anonymous module is set to nil
but in 1.8.7 it is set to the empty string (which, of course, is a
truthy value). This causes Rails to throw a "can't convert nil into
String" error when attempting to render views from an anonymous
controller.

We can fix this by declaring an anonymous controller's name *on
creation* and then evaluating the body of the controller with
`class_eval`.
f629d01
@mudge

We have separately filed a pull request in Rails to address the core issue (that of anonymous controllers being unable to determine a layout) but this workaround will still be required for versions of Rails prior to the fix (if it is accepted).

@justinko justinko closed this in 10bba44
@justinko

Thanks guys. Sorry this took so long, not sure how it slipped through the cracks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 2, 2012
  1. @mudge

    Fix render_views for anonymous controllers on 1.8.7.

    James Hunt & Paul Mucur authored mudge committed
    Rails uses a controller's class name to determine the layout to use when
    rendering views but it does so with an inherited hook. This means that
    setting the name of a class *after* it inherits is too late. This is not
    an issue in Ruby 1.9.2 as the name of an anonymous module is set to nil
    but in 1.8.7 it is set to the empty string (which, of course, is a
    truthy value). This causes Rails to throw a "can't convert nil into
    String" error when attempting to render views from an anonymous
    controller.
    
    We can fix this by declaring an anonymous controller's name *on
    creation* and then evaluating the body of the controller with
    `class_eval`.
This page is out of date. Refresh to see the latest.
View
30 features/controller_specs/anonymous_controller.feature
@@ -328,3 +328,33 @@ Feature: anonymous controller
"""
When I run `rspec spec`
Then the examples should all pass
+
+ Scenario: rendering views
+ Given a file named "spec/controllers/application_controller_spec.rb" with:
+ """
+ require "spec_helper"
+
+ describe ApplicationController do
+ render_views
+
+ controller do
+ def index
+ @name = "Bob"
+ end
+ end
+
+ describe "#index" do
+ it "greets the user" do
+ get :index
+ response.body.should =~ /Hello there, Bob/
+ end
+ end
+ end
+ """
+ And a file named "app/views/anonymous/index.html.erb" with:
+ """
+ Hello there, <%= @name %>
+ """
+ When I run `rspec spec`
+ Then the examples should all pass
+
View
6 lib/rspec/rails/example/controller_example_group.rb
@@ -58,10 +58,10 @@ def controller(base_class = nil, &body)
controller_class :
ApplicationController
- metadata[:example_group][:described_class] = Class.new(base_class, &body)
- metadata[:example_group][:described_class].singleton_class.class_eval do
- def name; "AnonymousController" end
+ metadata[:example_group][:described_class] = Class.new(base_class) do
+ def self.name; "AnonymousController"; end
end
+ metadata[:example_group][:described_class].class_eval(&body)
before do
@orig_routes, @routes = @routes, ActionDispatch::Routing::RouteSet.new
Something went wrong with that request. Please try again.