Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Incorrect binding used to render generator templates

#1493

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.

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

State: resolved
Milestone: none
Assigned to: josh josh
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user set to josh

    December 22nd, 2008 @ 11:44 AM

  2. josh
    josh
    • Milestone cleared.
    • State changed from new to open

    December 28th, 2008 @ 08:56 PM

  3. Repository
    Repository
    • State changed from open to resolved

    (from [1f0aecd931a9292b52402143be979ab4c06f06cd]) Allow custom rails generators to pass in their own binding to Create command so that the corresponding erb templates get rendered with the proper binding [#1493 Incorrect binding used to render generator templates state:resolved] http://github.com/rails/rails/co...

    December 28th, 2008 @ 09:14 PM