【发布时间】:2014-04-17 01:47:01
【问题描述】:
我想使用 Angular.js 和 Rails 显示卡片索引。
这是我的代码。
# app/views/decks/show.html.erb
<div ng-controller="DeckCtrl">
<form ng-submit="addCard()">
<input type="text" ng-model="newCard.front" />
<input type="submit" value="Add">
</form>
{{newCard.front}}
<ul>
<li ng-repeat="card in cards">
{{card.front}}
</li>
</ul>
</div>
还有咖啡脚本
# app/assets/javascripts/decks.js.coffee
app = angular.module("Deck", ["ngResource"])
@DeckCtrl = ($scope, $resource) ->
Card = $resource("/cards/:id", {id: "@id"}, {update: {method: "PUT"}})
$scope.cards = Card.query()
$scope.addCard = ->
$scope.cards.push($scope.newCard)
$scope.newCard = {}
还有CardsController。
class CardsController < ApplicationController
respond_to :json
def index
respond_with Card.all
end
def show
respond_with Card.find(params[:id])
end
def create
respond_with Card.create(card_params)
end
def update
respond_with Card.update(params[:id], card_params)
end
def destroy
respond_with_Card.destroy(params[:id])
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def card_params
params.require(:card).permit(:front, :back, :tips, :cardtype, :deck_id, :box_number)
end
end
addCard 函数正在运行,但 Card.query() 给我这个错误。
Started GET "/cards" for 127.0.0.1 at 2014-04-17 10:39:42 +0900
Processing by CardsController#index as HTML
Completed 406 Not Acceptable in 2ms
ActionController::UnknownFormat - ActionController::UnknownFormat:
看起来Angular.js 发送HTML 而不是json,这会导致错误,对吗?
如何消除此错误?
【问题讨论】:
标签: javascript ruby-on-rails angularjs