Friday, 6 September 2013

Getting has_many association from ngResource in rails

Getting has_many association from ngResource in rails

I'm trying to recreate the following erb template in angularjs using
ngResource, but can't seem to figure out how to access the event.spikes
(it's a has_many relationship).
The erb file I want to change:
<div class='event-list'>
<ul>
<% @events.each do |event| %>
<li><%= event.name %> || <small><%= event.date %></small>
<ul>
<% event.spikes.each do |spike| %>
<li>Time of Spike: <%= spike.date_time %> || Spike Velocity: <%=
spike.peak_velocity %></li>
<% end %>
</ul>
</li>
<% end %>
</ul>
</div>
The half finished .html without erb:
<div class='event-list' ng-controller='EventsCtrl'>
<ul>
<li ng-repeat='event in events'>
{{ event.name }} || <small>{{ event.date }}</small>
<ul>
<<<<<SOMETHING GOES HERE???>>>>>>>>
</ul>
</li>
</ul>
</div>
Here's my service, app/assets/javascripts/angular/controllers/event.js:
App.factory('Event', ['$resource', function($resource) {
return $resource('/events/:id', { id: '@id' });
}]);
My angular controller,
app/assets/javascripts/angular/controllers/events.ctrl.js:
App.controller('EventsCtrl', ['$scope', 'Event', function($scope, Event) {
$scope.events = Event.query();
}]);
And where my angular app is defined, app/assets/javascripts/app.js:
window.App = angular.module('sportsSpike', ['ngResource']);

No comments:

Post a Comment