I use a ranked model and follow along with this sorting of the tables .
I am pulling my hair out because of this, and everything that can be done to help would be fantastic!
I got Decksone that has_many Cards. And on the deck editing page, Im trying to sort my own set of cards.
card.rb
belongs_to :deck
include RankedModel
ranks :row_order, :with_same => :deck_id
routes.rb
resources :decks do
post :sort, on: :collection
resources :cards
end
decks_controller.rb
def sort
@deck = Deck.find(params[:id])
@deck.update_attributes(deck_params)
@deck.save
render nothing: true
end
def deck_params
params.require(:deck).permit(:name, :id, :cards_attributes => [:id, :question, :answer, :deck_id, :_destroy, :row_order, :row_order_position])
end
JQuery
$.ajax({
type: 'POST',
url: $(this).data('update_url'),
dataType: 'json',
data: { id: deck_id, deck: { cards_attributes: { row_order_position: position } }}
All this on Rails 4. I think the problem is how Im posting the data.
Ive checked row_orderon the cards themselves and installed it. I just can't figure out how to pass information through ajax with nested cards_attributes.
, row_order_position: position ajax, , row_order: position.