Search code examples
ruby-on-railsjsonsproutcore

json format from rails to sproutcore


For those of you using Rails as a backend to their Sproutcore clients, which one is the best way to format the data into json?

From the Sproutcore guides there was this approach:

 def as_json(options = {})
   event_hash = {
     "guid" => self.id,
     "id" => self.id,
     "designation" => self.designation,
     "category" => self.category,
     "scheduled_for" => self.scheduled_for,
     "location" => self.location,
     "groups" => self.groups,
     "resources" => self.resources
   }

   event_hash
 end

But it fails, send an "Illegal statement error". Then, I changed to this other method:

 def as_json(options = {})
   # event_hash = options.merge(:include => [:groups, :resources], :methods => :guid)
   event_hash = options.merge(:methods => :guid)
   super(event_hash)
 end

which seems to be working as far as the formatting is concerned, although I am suspecting it to causing some trouble regarding the representation in the dataHash of the store. Anyway, ha anyone been having similar issues with the first version of as_json? If not, is there anything I am doing wrong?

Appreciate any help


Solution

  • On the first method you need to call super:

    def as_json(options = {})
      event_hash = {
        "guid" => self.id,
        "id" => self.id,
        "designation" => self.designation,
        "category" => self.category,
        "scheduled_for" => self.scheduled_for,
        "location" => self.location,
        "groups" => self.groups,
        "resources" => self.resources
      }
    
      super(event_hash)
    end
    

    However you should get the options param and process to do this apropiately.