Prabhakaran
Prabhakaran

Reputation: 4013

emberjs assertion failed error

I am trying to insert data from ember's view but am getting following error message:

Uncaught Error: assertion failed: Your server returned a hash with the key refunds but you have no mapping for it

and here is my coding can anyone correct it.

My handlebar

<form>
   <th>{{view Ember.TextField valueBinding="refund_amount" placeholder="Enter refund amount"}}</th>
   <td><button type="submit" class="btn btn-success complete-btn" {{action saveRefund}}>Refund</button></td>
</form>

My js model

Office.Refund = DS.Model.extend({
    job_id: DS.attr('number'),
    customer_id: DS.attr('number'),
    amount: DS.attr('number')
});

MY js controller

saveRefund: function() {
    var refund = Office.Refund.createRecord({
    job_id: this.get('id'),
    customer_id: this.get('customer.id'),
    amount: this.get('refund_amount')
  });
    this.get('store').commit();
    refund.on('didCreate',function() {
    alert('created successfully');
  });
}

Here is my refund_controller.rb

  def index
    @refund = Thunderbolt::Refund.all
    respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @refund}
    end
  end
def new
    @refund = Refund.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @refund }
    end
  end

  def create
    refunds = params[:refund]
    @refund = Refund.new(job_id: refunds[:job_id], customer_id: refunds[:customer_id], amount: refunds[:amount])
    respond_to do |format|
      if @refund.save
        format.html { redirect_to @refund, notice: 'Refund successful.' }
        format.json { render json: @refund, status: :created, location: @refund }
      else
        format.html { render action: "new" }
        format.json { render json: @refund.errors, status: :unprocessable_entity }
      end
    end
  end

Here is my refund_serializer.rb

  class RefundSerializer < ActiveModel::Serializer
    attributes :id, :job_id, :customer_id, :amount, :created_at, :updated_at
  end

Here is my refund.rb model

  class Refund < ActiveRecord::Base
    attr_accessible :id, :amount, :customer_id, :job_id, :created_at, :updated_at
  end

Upvotes: 0

Views: 168

Answers (2)

Prabhakaran
Prabhakaran

Reputation: 4013

Solved this error by adding resources :refunds in routes.rb instead of

  get "refunds/index"

  get "refunds/new"

  get "refunds/edit"

  get "refunds/show"

Upvotes: 1

intuitivepixel
intuitivepixel

Reputation: 23322

Before the comments are getting to long I'll try to explain it in an answer. When you post a new record to be saved, your backend should return the new record with an id set to it like:

{
  "refund": {
    "id": 123,
    "job_id":691,
    "customer_id":424,
    "amount":1
  }
}

If job_id is your record's primary key the you need to have a mapping for it in your DS.RESTAdapter, do you?

Hope it helps.

Upvotes: 0

Related Questions