vendredi 24 juillet 2015

Rails API pass ActiveRecord object

I am trying to pass an entire record from one site to another. would rather not program it to make them all strings.

I want to access them like objects in my receiving website.

I have everything connected up and communicating correctly.

This is how i am tring to send this truck to

this is in the :after_save

def send_to_transport
    TransportTruckApi.create(self, self.user, self.company)
  end

this is the method that sends the data

  def self.create(truck_params, user_params, company_params, site = URL, api='/api/v1/trucks')
    conn = api_conn(site)
    conn.params = {'truck' => truck_params, 'user' => user_params, 'company' => company_params}
    response = conn.post api
    json_parse(response)
  end

when this activate i get this on receiving server:

Started POST "/api/v1/trucks?company=%23%3CCompany%3A0xc965ddc%3E&truck=%23%3CTruck%3A0xc5d30fc%3E&user=%23%3CUser%3A0xcb3baa8%3E" for 127.0.0.1 at 2015-07-24 09:21:47 -0500
Processing by TrucksController#create as */*
Parameters: {"company"=>"#<Company:0xc965ddc>", "truck"=>"#<Truck:0xc5d30fc>", "user"=>"#<User:0xcb3baa8>"}
WARNING: Can't verify CSRF token authenticity
   (74.5ms)  SELECT * FROM geometry_columns WHERE f_table_name='trucks'
Completed 500 Internal Server Error in 640ms

NoMethodError (undefined method `stringify_keys' for "#<Truck:0xc5d30fc>":String):
    activerecord (3.2.3) lib/active_record/attribute_assignment.rb:69:in `assign_attributes'
  activerecord (3.2.3) lib/active_record/base.rb:498:in `initialize'
  cancan (1.6.10) lib/cancan/controller_resource.rb:85:in `new'

If i have to send each attribute individually is there a way to simplify the code. so i don't have to define every attribute.

Aucun commentaire:

Enregistrer un commentaire