The right way to display one to many models using Angular and Rails

To get better understanding of how Angular works with Rails I am
creating a simple todo app.This is my first week with Angular and I have
no idea how to use it correctly with rails.

An idea is following. I have a list of todos and when I click on todo I
get a list of tasks below which are associated with this todo list. You
can take a look on attached picture, maybe it will explain better what I
am trying to say here.

DB:
One “todo” has many “tasks”. todo_id is a FK in the task model.

This is what I’ve done to display all todos:

{{todo.title}}

{{task.title}}

<-- form -->

As you see so far I get a list of tasks from todo but if I add a task or
delete one I have to reload a todo and I have no idea how to update html
on flight using Angular.

This is how I delete a todo (I hope it is not very ugly :slight_smile:
Todo = $resource("/api/todos/:id", {id: “@id”}, {update: {method:
“PUT”}})
$scope.todos = Todo.query()

$scope.deleteTodo = (id) ->
todo = (item for item in $scope.todos when item.id is id)[0]
Todo.remove(todo)
$scope.$apply(removeFromArray($scope.todos, todo))

I would really appreciate if someone could give me a good example of how
Angular lives with Rails and how to use “Angular.resource” correctly.
Meanwhile I will be praying and reading angular sources :slight_smile:

If somebody is interested in a solution - the easiest way is to use
properly $resource’s in Angular

Task = $resource("/api/todos/:todo_id/tasks/:id", {todo_id: “@todo_id”,
id:"@id"}, {update: {method:“PUT”}})

Task.query([params], success, error)
Task.save([params], postData, success, error)

as example

Task.save({todo_id:1,id:23}, {title:“new task”, todo_id:1})

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs