I have a controller:

class ItemsController < ApplicationController
respond_to :json

def index
items = Item.all

respond_with(items)
end
end

I am curling *my_app/items.json* and see a response with JSON object — just
as expected. At this point I don't have any views associated with
controller, specifically I don't have *index.html.erb* (*.html*).

Now if only I create an *index.haml* (for instance), with a simple *%h1
Hello, world!* line, requesting (again via curl) *my_app/items.json* returns
an html string with *<h1>Hello, world!</h1>*. Note that I didn't alter the
controller code — it just remains untouched.

I'm sure I'm missing something. Can anyone explain of what's going on here?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/G-BUP_w7OasJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Peter Vandenabeele at Dec 29, 2011 at 10:28 am

    On Thu, Dec 29, 2011 at 2:08 AM, Eugene Pirogov wrote:

    I have a controller:

    class ItemsController < ApplicationController
    respond_to :json

    def index
    items = Item.all

    respond_with(items)
    end
    end

    I am curling *my_app/items.json* and see a response with JSON object —
    just as expected. At this point I don't have any views associated with
    controller, specifically I don't have *index.html.erb* (*.html*).

    Now if only I create an *index.haml* (for instance), with a simple *%h1
    Hello, world!* line, requesting (again via curl) *my_app/items.json* **returns
    an html string with *<h1>Hello, world!</h1>*. Note that I didn't alter
    the controller code — it just remains untouched.

    I'm sure I'm missing something. Can anyone explain of what's going on here?
    Observation:

    When you rename the index.haml file to index.html.haml, it will work as
    expected
    (at least it works here and I could reproduce your problem).

    For json, the default built-in handler for the json format is used.
    For html, the index.html.haml template is used

    Speculation:

    I presume that Rails first checks if for json, a file with any of the
    standard
    handlers is present (from the error message below, that would be :erb,
    :builder, :coffee, :haml).

    Missing template orders/index, application/index with {:handlers=>[:erb,
    :builder, :coffee, :haml], :formats=>[:json], :locale=>[:en, :en]}.

    Now if it sees a file ending in .html.haml it is clear that this is a html
    file
    (and not an XML or a JSON file) and not use that file as a template.

    But for a file index.haml , I presume Rails accepts this also for rendering
    json,
    even without the .json.haml ending ...

    So, I created 2 additional files:

    peterv@e6500:~/b/app/views/orders$ vim index.json.haml
    peterv@e6500:~/b/app/views/orders$ rm index.json.haml
    peterv@e6500:~/b/app/views/orders$ vim index.haml
    peterv@e6500:~/b/app/views/orders$ rm index.haml

    And the picking order is clear:
    * highest priority: index.json.haml
    * lesser priority: index.haml
    * default: built-in json renderer
    * never used: index.html.haml

    HTH,

    Peter

    --
    Peter Vandenabeele
    http://twitter.com/peter_v
    http://rails.vandenabeele.com

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Eugene Pirogov at Dec 29, 2011 at 11:03 am
    Awesome! Thank you, Peter. Now it's clear to me of what's happening.

    Also I think the documentation worth mentioning this behavior.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/3F2CehZXKGMJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 29, '11 at 1:09a
activeDec 29, '11 at 11:03a
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase