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
4

2 回答 2

1

resources :refunds通过添加而routes.rb不是解决了这个错误

  get "refunds/index"

  get "refunds/new"

  get "refunds/edit"

  get "refunds/show"
于 2013-08-10T14:46:58.787 回答
0

在评论变得很长之前,我会尝试在答案中解释它。当您发布要保存的新记录时,您的后端应返回带有一id组的新记录,例如:

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

如果job_id您的记录的主键是您需要在您的 中对其进行映射DS.RESTAdapter,对吗?

希望能帮助到你。

于 2013-08-10T12:18:14.480 回答