4

I am having trouble figuring how to add drag and drop to a has_many through association?

I have a Model for Boards and each Board has many Lists.

Each List has many Cards and each Card has many Lists through a join model called ListCard

I am trying to add drag and drop to cards on each list. I have the front end working with jQuery UI i just don't know how to save the position through ajax to the position integer column in my ListCard Model.

I have looked at these below but i can't figure out how to setup the controller for a has_many through association?

Railscast for setting up drag and drop with acts_as_list

Using acts_as_list with has_many :through in 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 

Lists Controller

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

Board Show 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 ->
Community
  • 1
  • 1
EliteViper7777
  • 187
  • 1
  • 12

0 Answers0