Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

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

mudge
Copy link

@mudge 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.

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`.
@mudge
Copy link
Author

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
@justinko
Copy link
Contributor

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants