emilioforrer / haml_coffee_assets

Haml Coffee templates in the Rails asset pipeline or as Sprockets engine.
https://rubygems.org/gems/haml_coffee_assets
MIT License
438 stars 145 forks source link

cannot reach nested JSON objects? #67

Closed ellmo closed 12 years ago

ellmo commented 12 years ago

The Rabl-generated JSON passed to HAMLC template looks as follows

[
  {
    "comment": {
      "content" :  "<p>8796tuyghj</p>",
      "created_at" : "2012-08-24T21:18:13Z",
      "user" : {
        "login" : "ellmo"
      }
    }
  },
(...)
]

And the HAMLC template looks like this

.div-class
  #div-id
    - _.each @comments, (comment) ->
      .comment-div
        .comment-top
          .comment-author
            = comment.user.login
          .comment-date
            = $.timeago(comment.created_at)
        .comment-bottom
          .comment-content
            = comment.content

But I receive a javascript error Cannot read property 'login' of undefined. However when I change my HAMLC to

.div-class
  #div-id
    - _.each @comments, (comment) ->
      - console.log comment

I can see all json objects in the browsers console intact, so I know for a fact that even after being passed into the template, the JSON comment object contains user object.

Is it possible HAMLC has a problem reaching nested attributes, or am I doing something plain wrong here?

ellmo commented 12 years ago

Okay, I'm being an idiot twice during one day. Wow :flushed: The problem lied in RABLs structure that encapsulates all comment's attributes within comment attribute, god I hate that, that is so not Ruby.