带有角度的轨道4中的406(不可接受)

时间:2014-05-10 10:17:33

标签: angularjs ruby-on-rails-4

我正在使用rails 4和angularjs。我正在关注railscast(http://railscasts.com/episodes/405-angularjs)教程。 rails 3上的教程。 问题是我在尝试从数据库查询数据时遇到错误。这是错误" GET localhost:3000 /条目406(不可接受)"。我的角色代码工作正常,可能是问题出在我的控制器中。因为当我使用" localhost:3000 / entries"链接,我收到此错误" EntriesController中的ActionController :: UnknownFormat #index"。

控制器:

class EntriesController < ApplicationController
    respond_to :html, :json

      def index
        respond_with Entry.all
      end
end

角:

app = angular.module("Raffler", ["ngResource"])

app.factory "Entry", ["$resource", ($resource) ->
  $resource("/entries/:id", {id: "@id"}, {update: {method: "PUT"}})
]
@RaffleCtrl = ["$scope", "Entry", ($scope, Entry) ->
  $scope.entries = Entry.query()
]

视图:

<div ng-controller="RaffleCtrl">
  <form ng-submit="addEntry()">
    <input type="text" ng-model="newEntry.name">
    <input type="submit" value="Add">
  </form>

  <ul>
    <li ng-repeat="entry in entries">
      {{entry.name}}
      <span ng-show="entry.winner" ng-class="{highlight: entry == lastWinner}" class="winner">WINNER</span>
    </li>
  </ul>

  <button ng-click="drawWinner()">Draw Winner</button>
</div>

1 个答案:

答案 0 :(得分:11)

将此添加到您的路线配置

resources :entries, defaults: { format: 'json' }