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

JSON action renders template under AMD #80

Closed
bpardee opened this issue Jan 18, 2013 · 8 comments
Closed

JSON action renders template under AMD #80

bpardee opened this issue Jan 18, 2013 · 8 comments

Comments

@bpardee
Copy link

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
  end
end

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 127.0.0.1 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.

@netzpirat
Copy link
Collaborator

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?

@jimmycuadra
Copy link
Collaborator

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.

@netzpirat
Copy link
Collaborator

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

@bpardee
Copy link
Author

bpardee commented Jan 19, 2013

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

@jimmycuadra
Copy link
Collaborator

@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
Copy link
Collaborator

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

@netzpirat
Copy link
Collaborator

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

@bpardee
Copy link
Author

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

No branches or pull requests

3 participants