【发布时间】:2015-08-29 10:28:38
【问题描述】:
我有记录和类别模型:
class Record < ActiveRecord::Base
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :records
end
Category 模型有一个 name 字段,应该用于显示。我正在使用 ng-repeat 显示记录,'record.category.name' 导致我的页面中断:
<tr ng-repeat="record in records">
<td>{{record.category.name)}}</td>
<td>{{record.description}}</td>
<td>{{record.created_at | date:'medium'}}</td>
</tr>
如何正确检索“record.category”,以便像上面那样使用“名称”字段?到目前为止,使用 ngResource 并没有填充关联。
我正在使用 ngResource 检索 Record 模型:
var Record = $resource('/records/:recordId', {
recordId:'@id',
format: 'json'
}, {
'save': {
method: 'PUT'
},
'create': {
method: 'POST'
}
});
Record.query({
account_id: $routeParams.accountId
}, function(results) {
return $scope.records = results;
});
如何在 AngularJS 中检索此模型,以便关联数据也可用于显示?
感谢任何帮助。
编辑:
index.json.jbuilder
json.array! @records, partial: 'record', as: :record
_record.json.jbuilder
json.(record, :id, :account_id, :user_id, :amount, :date, :description, :is_expense, :category_id, include: [:category])
【问题讨论】:
标签: ruby-on-rails angularjs ruby-on-rails-4 associations ngresource