在Ember js中在索引页面上创建记录

时间:2013-03-21 19:44:13

标签: ruby-on-rails ember.js

我想在索引页面上生成创建记录而无需转到其他路径。编辑部分成功运作。当我点击添加记录时,我有500错误。它说

undefined method `income_url' for #<Api::IncomesController:0x007fcf1f6407f8>

我在索引页面上使用视图渲染的create.handlebars是

<div class="control-group">
  <label class="control-label">Income name</label>
  <div class="controls">
    {{view Ember.TextField valueBinding="newIncomeName"}}
  </div>
</div>

<div class="control-group">
  <div class="controls">
    <input type="submit" value="Add" {{action submit content}}>
  </div>
</div>

我的route.js.coffee是:

EmberMoney.Router.reopen
  location: 'history'

EmberMoney.Router.map ->
  @resource 'incomes', ->
    @route 'index' # this route is used for creating new records

EmberMoney.IncomesRoute = Ember.Route.extend
  model: ->
    EmberMoney.Income.find()

EmberMoney.IncomesEditRoute = Ember.Route.extend
  setupController: (controller, model) ->
    if model.get('transaction') == @get('store').get('defaultTransaction')
      transaction = @get('store').transaction()
      transaction.add model
    controller.set('content', model)

  deactivate: ->
    @modelFor('incomes.edit').get('transaction').rollback()

  events:
    submit: (record) ->
      record.one 'didUpdateRecord', =>
        @transitionTo 'index'
      record.get('transaction').commit()

EmberMoney.IncomesIndexRoute = Ember.Route.extend
  model: ->
    EmberMoney.Income.createRecord()
  setupController: (controller, model) ->
    controller.set('content', model)

  events:
    submit: (record) ->
      record.on "didCreate", =>
        @transitionTo 'index'
      record.get('transaction').commit()

我的api / incomes_controller.rb是:

class Api::IncomesController < ApplicationController
    respond_to :json

    def index
      respond_with Income.all
    end

    def show
      respond_with Income.find(params[:id])
    end

    def create
      respond_with Income.create(params[:income])
    end

    def update
      respond_with Income.update(params[:id], params[:income])
    end

    def destroy
      respond_with Income.destroy(params[:id])
    end
end

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在这种情况下,您需要为响应者指定名称空间。尝试: http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html

class Api::IncomesController < ApplicationController
    respond_to :json

    def index
      respond_with :api, Income.all
    end

    def show
      respond_with :api, Income.find(params[:id])
    end

    def create
      respond_with :api, Income.create(params[:income])
    end

    def update
      respond_with :api, Income.update(params[:id], params[:income])
    end

    def destroy
      respond_with :api, Income.destroy(params[:id])
    end
end