0

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
6
  • can you share the format of your JSON response, for the case of a single and multiple records? Commented Aug 8, 2013 at 14:12
  • Am getting alert as created successfully but actually not created. It is just empty. This is was am getting while seeing console {"refunds":[]} Commented Aug 8, 2013 at 14:15
  • In firefox console url is POST http://localhost:8080/refunds and source is {"refund":{"job_id":691,"customer_id":424,"amount":11}} Commented Aug 10, 2013 at 12:01
  • I can see no id field in your response JSON, do you have a mapping to job_id or something defined for it? Commented Aug 10, 2013 at 12:03
  • This is a POST not response({"refund":{"job_id":691,"customer_id":424,"amount":11}}) and my response is {"refunds":[]} Commented Aug 10, 2013 at 12:08

2 Answers 2

1

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

  get "refunds/index"

  get "refunds/new"

  get "refunds/edit"

  get "refunds/show"
Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

job_id is not a primary key. And i dont have any mapping also.
@RelicSet, so my answer still applies, your JSON response is wrong and should have the format mentioned in the answer
Record is not saving thats why no reponse. But i cant find where i am wrong. can you help
@RelicSet, I'm sorry I'm not so much the rails expert :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.