I’ve been using ranked model and following along with this sortable tables tutorial.
I’m pulling my hair out over this, and anything anyone can do to help would be fantastic!
I’ve got Decks which has_many Cards. And on the decks edit page, I’m trying to sort its 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 } }}
This is all on Rails 4. I think the problem is how I’m posting the data.
I’ve checked row_order on the cards themselves, and it's being set. I just can’t seem to understand how to pass the information via ajax with the nested cards_attributes.
I'm also unsure why the tutorial shows row_order_position: position in the ajax post, when I thought it would just be row_order: position.