This project is archived and is in readonly mode.

#1493 ✓resolved
Matt Gadda

Incorrect binding used to render generator templates

Reported by Matt Gadda | November 29th, 2008 @ 07:17 AM

As of commit 5fa0457542b0ff541d0a80ff8c3561eec8e35959 (edge), Rails::Generator::Commands::Create appears to be passing the wrong binding to ERB.result.

  1. Create a custom generator derived from either Rails::Generator::Base or Rails::Generator::NamedBase
  2. Create a simple template and add the obligatory m.template() method call from manifest.
  3. Create a test method in your custom generator that excepts a block parameter and yields to that block somewhere in its body.
  4. Invoke that method from within the template and pass a block to it.

Note that although the method is callable, the block is never passed to the method on invocation. Any attempts to yield that block fail because &block is nil (whether implicitly or explicitly passed in). This is because the binding in which the template is rendered corresponds to the command, not the generator.

In my patch, I just provided for this case in Commands::Create.template() such that callers may pass in an optional :binding parameter. If passed in, template() uses the Generator binding and method invocations requiring blocks from within the template work fine.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket



Referenced by