Rails and Backbone.js: confused on how to setup portion of app
In my app, a user can login and navigate to a page where they can check
off tasks that they have completed.
View:
<div class="container">
<div class="row">
<div class="span12">
<table class="table table-hovered">
<tr>
<td></td>
<td>Resources</td>
<td>Deadline</td>
</tr>
<% @tasks.each do |task| %>
<tr id="<%= task.id %>" class="<%=
current_user.completed_tasks.include?(task) ? 'grey-row' : ''
%>">
<td class="span8">
<%= form_for find_or_create_association_for_task(task,
current_user), remote: true do |f| %>
<%= f.check_box :complete %> <%= task.item %>
<% end %>
</td>
<td>
<ul>
<% task.resources.each do |r| %>
<li><%= link_to_if r.link.present?, r.name, r.link,
target: "_blank" %></li>
<% end unless task.resources.blank? %>
</ul>
</td>
<td><%= task.deadline %></td>
</tr>
<% end %>
</table>
</div>
</div>
</div>
Find or create association for task helper method:
module TasksHelper
def find_or_create_association_for_task(task, athlete)
@task = AthleteTask.where(athlete_id: athlete, task_id: task.id).first
@task ||= athlete.athlete_tasks.create(task_id: task.id)
end
end
Athlete Model:
has_many :tasks, through: :athlete_tasks
has_many :athlete_tasks
Task Model:
has_many :athletes, through: :athlete_tasks
has_many :athlete_tasks
An Athlete accesses this page by logging in and navigating to /athlete/tasks
When an athlete checks off a task as 'complete', a remote call is fired
off to the Athlete::TasksController
class Athlete::TasksController < ApplicationController
before_filter :authenticate_user!
authorize_resource
respond_to :html, :json
def index
@tasks = Task.where(classification: current_user.classification)
respond_with @tasks
end
def update
@task = current_user.athlete_tasks.find(params[:id])
@task.update_attributes(params[:athlete_task])
end
end
I think my confusion is coming from how to properly load in the tasks that
should be loaded into the view. Any help would be greatly appreciated!!
No comments:
Post a Comment