ccocchi / rabl-rails

Rails 4.2+ templating system with JSON, XML and Plist support.
MIT License
208 stars 51 forks source link

Object 'nil' with extends #74

Closed BenSwennen closed 8 years ago

BenSwennen commented 9 years ago

When using extends I'm getting the following error:

[FATAL] [2015-06-03 15:03]   ActionView::Template::Error (no implicit conversion of nil into String):
    1: collection :@contacts
    2: 
    3: extends 'contacts/show'
  app/views/contacts/index.json.rabl:3:in `_app_views_contacts_index_json_rabl__1238373977362342878_70108584054580'
  app/controllers/api/v1/contacts_controller.rb:5:in `index'

contacts/index.json.rabl:

collection :@contacts
extends 'contacts/show'

contacts/show.json.rabl

object :@contact
node(:id)        { |contact| contact.id.to_s }
attributes       :name

If I render only index, it works perfectly, but after rendering show and then rendering index again, it gives me the error. If I then change node(:id) { |contact| contact.id.to_s } to node(:id) { |c| c.id.to_s }it works again, until I render the show. If I remove node(:id) { |contact| contact.id.to_s }it works perfectly normal Index also works if I do object :contact(w/o the @) in show.json.rabl but then ofcourse the show method fails.

Kaschman commented 9 years ago

I was having the same issue and found a workaround:

Add a third file, I called it 'base', include what used to be in show contacts/base.json.rabl node(:id) { |contact| contact.id.to_s } attributes :name

have both index and show extend base

contacts/index.json.rabl collection :@contacts extends 'contacts/base'

contacts/show.json.rabl object :@contact extends 'contacts/base'