JSON action renders template under AMD #80

bpardee opened this Issue Jan 18, 2013 · 8 comments


None yet

3 participants

bpardee commented Jan 18, 2013

If I have an action in my controller like the following:

class PatientsController < ApplicationController
  respond_to :json

  def show
    respond_with Patient.find(params[:id]).as_json

And I have a template with the same path at app/assets/javascripts/templates/patients/show.hamlc, then the server renders the template instead of json:

Started GET "/api/patients/368.json" for at 2013-01-18 09:58:40 -0500
Processing by PatientsController#show as JSON
  Parameters: {"id"=>"368"}
  Patient Load (7.5ms)  SELECT "patients".* ...
  Rendered app/assets/javascripts/templates/patients/show.hamlc (528.2ms)
Completed 500 Internal Server Error in 1018ms

ActionView::Template::Error (ReferenceError: Can't find variable: define):
    1: - patient            = @model

If I put a rabl template at app/views/patients/show.json.rabl, then it works okay.


Oops, that's no good :P Looks like a bug with the ActionView template handler. Please use the gem version 1.9.1 until fixed.

@jimmycuadra Can you have a look at this?


I'm investigating this, but haven't found the solution yet. For a quick, temporary fix:

  • In config/initializers/mime_types.rb, add:

    Mime::Type.register_alias "text/html", :hamlc
  • Change the name of the view file to show.hamlc.hamlc.

I hope to have a non-hack solution soon.


Thanks a lot for having a look and keeping us updated.

bpardee commented Jan 19, 2013

Thanks, as @netzpirat suggested I am using 1.9.1 without issues.


@netzpirat AFAICT a template file which does not specify a MIME type will be used for all MIME types if a MIME-type-specific template is not found. It may be necessary for templates shared between Rails and Sprockets to use the action.format.handler convention.

This creates a problem with Sprockets, because we'd have to register the html MIME type with Sprockets, but set the content type to application/javascript so that it would still process it as JS. Doesn't seem very clean to me. I'm not sure how to proceed. Thoughts?

@jimmycuadra jimmycuadra added a commit that closed this issue Jan 20, 2013
@jimmycuadra jimmycuadra Add custom resolver to prevent Rails from rendering Haml Coffee templ…
…ates for non-HTML formats. Fixes #80.

Figured out how to do this by using a custom ActionView::Resolver to do the template lookup for the templates directory. :D


Thanks a lot for the nice fix @jimmycuadra! Version 1.11.0 released.

bpardee commented Jan 20, 2013

Thanks! I've updated and all looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment