Retrieve associations in AngularJS & Rails using ngResource -
i have record & category model:
class record < activerecord::base belongs_to :category end class category < activerecord::base has_many :records end
the category model has name field should used display. using ng-repeat display records, 'record.category.name' causes page break:
<tr ng-repeat="record in records"> <td>{{record.category.name)}}</td> <td>{{record.description}}</td> <td>{{record.created_at | date:'medium'}}</td> </tr>
how can retrieve 'record.category' 'name' field available have above? using ngresource far has not populated associations.
i retrieving record model using ngresource:
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; });
how can retrieve model in angularjs association data available display well?
any appreciated.
edit:
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])
this isn't angular issue, it's rails (and api design) issue. reason api design because depending on use case may more efficient client fetch categories , work in front end match categories records. said, you're trying need change rails app.
modified code linked answer:
def show @record = record.find(params[:id]) respond_to |format| format.json { render :json => @record.to_json(:include => :category) } end end
back when doing rails :include
in find
call haven't done rails time. don't know if that's still best way it.
Comments
Post a Comment