我无法确定如何通过关联添加拖放到has_many?
我有一个董事会模型,每个董事会都有很多名单。
每个列表都有许多卡片,每张卡片通过名为ListCard
的连接模型有许多列表我正在尝试添加拖放到每个列表上的卡片。我的前端使用jQuery UI我只是不知道如何通过ajax将位置保存到我的ListCard模型中的位置整数列。
我已经看过以下这些但我无法弄清楚如何通过关联设置控制器的has_many?
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
模型
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
路线
resources :boards do
resources :lists do
collection { post: sort }
end
end
resources :cards
列出控制器
def sort
# not sure what to put here
render nothing: true # this is a POST action, updates sent via AJAX
end
Board Show Page(例如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可排序Coffescript
jQuery ->
$('cardwrap').sortable
axis: 'y'
update ->