0

I have a simple rails todo list running. I have 2 relevant models, List and Task.

A List obviously has_many :tasks 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.

I believe the issue may be in the url I'm passing to my lists.js.coffee.

The output after a drag and drop-

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"=>"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)

My routes-

  resources :lists do
    resources :tasks do
      collection { post :sort }
    end
  end

My lists.js.coffee-

jQuery ->
  $('#incomplete').sortable
    axis: 'y'
    update: ->
     $.post($(this).data('update-url'), $(this).sortable('serialize'))

My task controller-

class TasksController < 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

And finally the show view of the List where I'm rendering complete and incomplete tasks-

<ul class="task-list" id="incomplete" data-update-url="<%= sort_list_tasks_url(@list)%>">
    <% @list.tasks.incomplete.each do |task| %> 
     <table class="table table-hover table-condensed">
       <tbody>
         <tr>
           <td>
            <li id="task-<%= task.id %>">
              <span class="task-description" id="description-<%= task.id %>">
                <%= image_tag task.user.gravatar_url, :class => "gravatar_index" %>
                <%= task.description %>
              </span>
              <span class="pull-left" id="completed_checkbox">
               <%= button_to "Completed", complete_task_path(@list.id,task.id), :class => "btn-mini", remote: true %>
              </span>
            </li>
          </td>
         </tr>
       </tbody>
      </table>
    <% end %>

I have been following Rbates tutorial that handles only one faq model, 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.


[UPDATE]

I believe I have made progress in moving some of the logic into my lists controller and cleaning up the list show view-

lists controller-

  def show
    @list = List.find(params[:id])  
    @tasks = @list.tasks.incomplete.order("position")
  end

  def sort
    render nothing: true
  end

List show view-

<ul class="task-list" id="incomplete" data-update-url="<%= sort_list_tasks_url %>">
    <% @tasks.each do |task| %> 
     <table class="table table-hover table-condensed">
       <tbody>
         <tr>
           <td>
            <%= content_tag_for :li, task do %>
              <span class="task-description" id="description-<%= task.id %>">
                <%= image_tag task.user.gravatar_url, :class => "gravatar_index" %>
                <%= task.description %>
              </span>
              <span class="pull-left" id="completed_checkbox">
               <%= button_to "Completed", complete_task_path(@list.id,task.id), :class => "btn-mini", remote: true %>
              </span>
            <% end %>
          </td>
         </tr>
       </tbody>
      </table>
    <% end %>
  </ul>

However, now I'm getting a

No route matches {:action=>"sort", :controller=>"tasks"}

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.

Progress (I think), but I'm still stuck.

4

1 回答 1

0

您可以通过在<ul>标签中添加 update-url 列表来解决它:

data-update-url="<%= sort_list_tasks_url(@list) %>"
于 2013-03-19T18:44:13.337 回答