ccocchi / rabl-rails

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

Failed to render template #3

Closed shmeltex closed 11 years ago

shmeltex commented 11 years ago

Following template fails to render:

object false

child :notes => :data do
   extends "notes/_single"
end

node(:success) { true }

It fails with error

ActionView::Template::Error: undefined method `notes` for nil:NilClass
ccocchi commented 11 years ago

@shmeltex child should be use to render data associated with the parent model (i.e. it will try to call a method or a relation in your model). In your example, there is no model since you used object false.

If you want to render the @notes instance variable you should use

child(:@notes => :data, :partial => 'notes/_single')

Thanks for the PR anyway !

shmeltex commented 11 years ago

@ccocchi I used your suggestion and it still fails with

ActionView::Template::Error: undefined method `notes` for nil:NilClass

My template looks like this now:

object false

child({:notes => :data}, :partial => "notes/_single")

node(:success) { true }

Just to put it into the context, I'm trying to get to the following output:

{
  "data": [
    { 
      "id": 1, 
      "note": "Note 1"
    },
    { 
      "id": 2, 
      "note": "Note 2"
    }
  ],
  "success": true
}
ccocchi commented 11 years ago

@shmeltex You missed the @ before notes to reference an instance variable => :@notes ;)

shmeltex commented 11 years ago

@ccocchi Actually, I didn't. In the controller I'm using decent_exposure gem to expose data to views as methods instead of instance variables.

Here is my controller:

class NotesController < ApplicationController
  respond_to :json

  expose(:notes) { Note.all }

  def index
    respond_with(notes)
  end
end
ccocchi commented 11 years ago

Hmm. I'll check how _decentexposure passed variables to the view. Currently, RablRails copies instances variables from @assigns variables. Maybe _decentexposture does not use this ivar.

ccocchi commented 11 years ago

It should be fixed with 8376c0d974cb21bd608bebc0a9dde52aae7aa5c2. @shmeltex Can you confirm please ?

shmeltex commented 11 years ago

Confirmed, works with both

object false

child({:notes => :data}, :partial => "notes/_single")

node(:success) { true }

and

object false

child :notes => :data do
   extends "notes/_single"
end

node(:success) { true }

Thank you for your work on the gem!

ccocchi commented 11 years ago

Great ! I've just released a new version with this fix. Thank you for the report !