How do I make act_as_list work with has_many through an association?

I am having a hard time figuring out how to add drag and drop to has_many via association?

I have a board model and each board has a lot of lists.

Each list has many Cards and each card has many lists via a connection model called ListCard

I am trying to add drag and drop to the cards in each list. I have an interface working with jQuery UI, I just don't know how to store the position via ajax in an integer position column in my ListCard model.

I've reviewed these below, but I can't figure out how to set up a controller for has_many via an association?

Railscast to customize drag with act_as_list

Using act_as_list with has_many: across rails

https://github.com/swanandp/acts_as_list/issues/95

https://github.com/swanandp/acts_as_list/issues/86

Models

class List < ActiveRecord::Base
    belongs_to :user
    belongs_to :board

    has_many :list_cards, dependent: :destroy
    has_many :cards, through: :cards
    accepts_nested_attributes_for :list_cards
end

class ListCard < ActiveRecord::Base
  belongs_to :list
  belongs_to :card

  acts_as_list :scope => :list_card
end

class Card < ActiveRecord::Base
    belongs_to :user

    has_many :list_cards
    has_many :lists, through: :list_cards
    accepts_nested_attributes_for :list_cards
end

      

Routes

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

resources :cards 

      

List Controller

def sort
  # not sure what to put here
  render nothing: true # this is a POST action, updates sent via AJAX
end

      

Show Board page (for example, www.example.com/board/1)

<% @lists.each do |list| %>
  <ul id="cardwrap">

     <%= list.title %> 

     <% list.cards.each do |card| %>

     <%= content_tag_for :li, card do %>
       <%= card.title %>
     <% end %>

  </ul>
<% end %> 

      

jQuery UI Sortable Coffescript

jQuery -> 
    $('cardwrap').sortable
      axis: 'y'
      update ->

      

+3


source to share





All Articles