This project is archived and is in readonly mode.

#2414 ✓invalid
Jeff Dean

Custom template handlers are broken in test (and maybe production)

Reported by Jeff Dean | April 4th, 2009 @ 06:38 AM | in 2.x

If you create a custom template handler, like the one described in Agile Web Development with Rails (.rdoc), the code examples will not work in the test environment.

When Rails is loaded in tests, by default it uses ActionView::Template::EagerPath to discover the files. When loaded in development, by default is uses ActionView::ReloadableTemplate. ReloadableTemplate correctly finds the custom template.

I've included a failing test that demonstrates this, but I'm at a loss as to how to fix it. Every solution I've found so far to make it pass makes several other tests fail, most of which relate to layout discovery or caching.

When I ran a similar example in production environment locally, it also could not find the file - so I believe this problem may affect custom template handlers in production as well.

Comments and changes to this ticket

  • thedarkone

    thedarkone April 4th, 2009 @ 06:00 PM

    Hi Jeff, your test doesn't pass because you are messing up controller_path, if you comment out the line everything works fine.

    Could you please make a smallish 1 controller, 1 view app on Github (or simply zip and attach it here) to demonstrate the issue?

  • Jeff Dean

    Jeff Dean April 4th, 2009 @ 08:30 PM

    Thanks for taking a look at this so quickly - I see what you mean about the view paths. I've attached a sample app that demonstrates the problem:

    Run in development environment:

    • script/server
    • open localhost:3000
    • there are 4 links on the homepage - clicking each one you will see "this works"

    Run in production environment:

    • script/server -e production
    • open localhost:3000
    • clicking each link on the homepage you will see that none say "this works"

    Run the tests:

    • ruby test/functional/third_party_extension_controller_test.rb
    • you'll see the same results as the production environment

    To get this to work, I created and registered a very simple template handler that looks like this:

    
    class CustomTemplateHandler < ActionView::TemplateHandler
      def render(template, assigns) 
        "it works"
      end
    end
    

    I spent some time trying to reproduce this in the the actionpack tests, but I haven't made any progress.

  • Jeff Dean

    Jeff Dean April 4th, 2009 @ 08:33 PM

    Quick note - the sample app has one controller with 4 actions:

    • Just a template (no action)
    • An empty action with a template
    • Using render :template with no extension
    • Using render :template with an extension

    So this issue seems to affect multiple ways of rendering templates with custom template handlers.

  • thedarkone

    thedarkone April 5th, 2009 @ 03:12 AM

    Thanks for the app, the reason that it all breaks down for you is that you are registering your template handler after the Rails has initialized (and in production loaded and compiled all the templates).

    You need to move the "handle-registering" code into an initilizer (config/initializers) or into Rails::Initializer.run do |config|; end block.

  • Jeff Dean

    Jeff Dean April 5th, 2009 @ 03:47 PM

    Thanks again for taking the time to look at this. That did the trick. This can be closed.

  • CancelProfileIsBroken

    CancelProfileIsBroken June 8th, 2009 @ 07:41 PM

    • State changed from “new” to “invalid”

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages