use*_*155 2 angularjs ruby-on-rails-4
我正在使用rails 4和angularjs.我正在关注railscast(http://railscasts.com/episodes/405-angularjs)教程.有关rails 3的教程问题是,当我尝试从数据库查询数据时,我收到错误.这是错误"GET localhost:3000/entries 406(Not Acceptable)".我的角色代码工作正常,可能是问题出在我的控制器中.因为当我使用"localhost:3000/entries"链接时,我收到此错误"EntriesController #index中的ActionController :: UnknownFormat".
控制器:
class EntriesController < ApplicationController
respond_to :html, :json
def index
respond_with Entry.all
end
end
Run Code Online (Sandbox Code Playgroud)
角度:
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()
]
Run Code Online (Sandbox Code Playgroud)
视图:
<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>
Run Code Online (Sandbox Code Playgroud)
use*_*155 11
将其添加到您的路线配置中
resources :entries, defaults: { format: 'json' }
Run Code Online (Sandbox Code Playgroud)