2

我想在索引页面上生成创建记录而不去其他路线。编辑部分工作成功。当我单击添加记录时,出现 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

非常感谢您的帮助。

4

1 回答 1

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
于 2013-04-06T16:24:34.233 回答