Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Fix render_views for anonymous controllers on 1.8.7. #525

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants

mudge commented Apr 2, 2012

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.

@mudge 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 commented Apr 18, 2012

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 Apr 18, 2012

Contributor

justinko commented Apr 18, 2012

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