title: Drag and drop sorting with multiple models rails
body: <p>I have a simple rails todo list running. I have 2 relevant models, <code>List</code> and <code>Task</code>.</p>
  
  <p>A <code>List</code> obviously <code>has_many :tasks</code> and now I am trying to use jQuery drag and drop to sort the position of incomplete tasks in the show view of the List and have the position order remain after a page load.  </p>
  
  <p>I believe the issue may be in the url I'm passing to my lists.js.coffee. </p>
  
  <p>The output after a drag and drop-</p>
  
  <pre><code>Started POST "/lists/1/tasks/sort" for 127.0.0.1 at 2012-11-04 11:08:37 -0800
  Processing by TasksController#sort as */*
    Parameters: {"list_id"=&gt;"1"}
    List Load (0.2ms)  SELECT "lists".* FROM "lists" WHERE "lists"."id" = ? LIMIT 1  [["id", "1"]]
    Rendered text template (0.0ms)
  Completed 200 OK in 5ms (Views: 1.7ms | ActiveRecord: 0.2ms)
  </code></pre>
  
  <p>My routes-</p>
  
  <pre><code>  resources :lists do
      resources :tasks do
        collection { post :sort }
      end
    end
  </code></pre>
  
  <p>My lists.js.coffee-</p>
  
  <pre><code>jQuery -&gt;
    $('#incomplete').sortable
      axis: 'y'
      update: -&gt;
       $.post($(this).data('update-url'), $(this).sortable('serialize'))
  </code></pre>
  
  <p>My task controller-</p>
  
  <pre><code>class TasksController &lt; ApplicationController
    before_filter :find_list
    respond_to :html, :js
  
    def index
      @task = List.tasks.incomplete.order("position")
    end
  
    def sort
      render nothing: true
    end
  
    def create
      @task = @list.tasks.new(params[:task])
      if @task.save
        flash[:notice] = "Task Created"
      else
        flash[:error] = "It just didn't happen for you"
      end
  
      respond_with @list
    end
  
    def complete
      @task = @list.tasks.find(params[:id])
  
      @task.update_attributes({ 
        completed: true
      })
  
      respond_with @list
    end
  
    private
  
    def find_list
      @list = List.find(params[:list_id])
    end
  end
  </code></pre>
  
  <p>And finally the show view of the List where I'm rendering complete and incomplete tasks-</p>
  
  <pre><code>&lt;ul class="task-list" id="incomplete" data-update-url="&lt;%= sort_list_tasks_url(@list)%&gt;"&gt;
      &lt;% @list.tasks.incomplete.each do |task| %&gt; 
       &lt;table class="table table-hover table-condensed"&gt;
         &lt;tbody&gt;
           &lt;tr&gt;
             &lt;td&gt;
              &lt;li id="task-&lt;%= task.id %&gt;"&gt;
                &lt;span class="task-description" id="description-&lt;%= task.id %&gt;"&gt;
                  &lt;%= image_tag task.user.gravatar_url, :class =&gt; "gravatar_index" %&gt;
                  &lt;%= task.description %&gt;
                &lt;/span&gt;
                &lt;span class="pull-left" id="completed_checkbox"&gt;
                 &lt;%= button_to "Completed", complete_task_path(@list.id,task.id), :class =&gt; "btn-mini", remote: true %&gt;
                &lt;/span&gt;
              &lt;/li&gt;
            &lt;/td&gt;
           &lt;/tr&gt;
         &lt;/tbody&gt;
        &lt;/table&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p>I have been following Rbates tutorial <a href="http://railscasts.com/episodes/147-sortable-lists-revised?" rel="nofollow">that handles only one faq model</a>, so I am missing the connection in how to make it work across both. After more than a couple hours I've become stuck. Thanks for taking a look and your attention. </p>
  
  <hr>
  
  <h2><strong>[UPDATE]</strong></h2>
  
  <p>I believe I have made progress in moving some of the logic into my lists controller and cleaning up the list show view-</p>
  
  <p>lists controller-</p>
  
  <pre><code>  def show
      @list = List.find(params[:id])  
      @tasks = @list.tasks.incomplete.order("position")
    end
  
    def sort
      render nothing: true
    end
  </code></pre>
  
  <p>List show view-</p>
  
  <pre><code>&lt;ul class="task-list" id="incomplete" data-update-url="&lt;%= sort_list_tasks_url %&gt;"&gt;
      &lt;% @tasks.each do |task| %&gt; 
       &lt;table class="table table-hover table-condensed"&gt;
         &lt;tbody&gt;
           &lt;tr&gt;
             &lt;td&gt;
              &lt;%= content_tag_for :li, task do %&gt;
                &lt;span class="task-description" id="description-&lt;%= task.id %&gt;"&gt;
                  &lt;%= image_tag task.user.gravatar_url, :class =&gt; "gravatar_index" %&gt;
                  &lt;%= task.description %&gt;
                &lt;/span&gt;
                &lt;span class="pull-left" id="completed_checkbox"&gt;
                 &lt;%= button_to "Completed", complete_task_path(@list.id,task.id), :class =&gt; "btn-mini", remote: true %&gt;
                &lt;/span&gt;
              &lt;% end %&gt;
            &lt;/td&gt;
           &lt;/tr&gt;
         &lt;/tbody&gt;
        &lt;/table&gt;
      &lt;% end %&gt;
    &lt;/ul&gt;
  </code></pre>
  
  <p>However, now I'm getting a </p>
  
  <pre><code>No route matches {:action=&gt;"sort", :controller=&gt;"tasks"}
  </code></pre>
  
  <p>error and I'm not sure why. The update-data-url attribute has a url I got directly from running rake routes, so I'm not sure why I'm getting this error. </p>
  
  <p>Progress (I think), but I'm still stuck. </p>