views:

20

answers:

0

I want to update two table of my database with a http post request (JSON format) from my android app.

my models look like this:

class Ecg < ActiveRecord::Base
  has_one :record, :as => :payload 
end



class Record < ActiveRecord::Base
    belongs_to :payload , :polymorphic => true
    belongs_to :user
end

an my record cotroller looks like this:

def new
    @ecg = Ecg.new()
    @record = Record.new(:user_id => current_user.id)

    respond_to do |format|
    format.html # new.html.erb
    end
end
def create
    @ecg = Ecg.new(params[:ecg])
    @ecg.build_record(params[:record])
    respond_to do |format|
        if @ecg.save
           format.html { redirect_to :action => 'new'}
           format.xml  { render :xml => @ecg, :status => :created, :location => @ecg }
           format.json { render :json => @ecg, :status => :created, :location => @ecg}
        else
           format.html { render :action => "new" }
           format.xml  { render :xml => @ecg.errors, :status => :unprocessable_entity }
           format.json { render :json => @ecg.errors, :status => :unprocessable_entity}
        end
   end
end

my html view looks like this:

<% form_for @record do |record|%>
<p>Trace ID: </p>
<%= record.text_field :trace_id%><br>
<p>Trace Timestamp:</p>
<%= record.datetime_select :trace_timestamp%>
<p>Record Timestamp: </p>
<%= record.datetime_select :record_timestamp%>
<p>Record Time Interval </p>
<%= record.text_field :record_time_interval%>
<%= record.hidden_field :user_id%><br>
<%fields_for @ecg do |ecg|%>
    <p>Lead 1: </p>
    <%= ecg.text_field :lead_1%><br>
    <p>Lead 2: </p>
    <%= ecg.text_field :lead_2%><br>
    <p>Lead 3: </p>
    <%= ecg.text_field :lead_3%><br>
<%end%>
<%= record.submit 'Create'%>
<%end%>

if i create a new record from the view this works fine.

Now my Question:

How should I format my json-post to create an new record? I tried different format with curl but without any success :-(

for example:

curl -H "Content-Type:application/json" -H "Accept:application/json" -d "{\"ecg\": {\"lead_1\":\"234234\", \"lead_2\":"8686", \"lead_3\":\"987689\", \"record\":{\"record_time_interval\":\"63.333\", \"record_timestamp(2i)\":\"9\", \"trace_timestamp(5i)\":\"58\", \"record_timestamp(3i)\":\"29\", \"trace_id\":\"1\", \"record_timestamp(4i)\":\"21\", \"record_timestamp(5i)\":\"58\", \"user_id\":\"7\", \"trace_timestamp(1i)\":\"2010\", \"trace_timestamp(2i)\":\"9\", \"trace_timestamp(3i)\":\"29\", \"record_timestamp(1i)\":\"2010\", \"trace_timestamp(4i)\":\"21\"}}}" http://locahost:3000/records

this gives me the following error:

ActiveRecord::AssociationTypeMismatch (Record(#2174513760) expected, got >HashWithIndifferentAccess(#2148143560)): app/controllers/records_controller.rb:24:in new' app/controllers/records_controller.rb:24:increate'

i would really appreciate some help.