Using a rank model with rails 4

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.

+4
1

( , ):


, ?

, , position , ajax

, , ? DeckPosition, :

#app/models/deck_position.rb
Class DeckPosition < ActiveRecord::Base
    belongs_to :deck, :class_name => 'Deck'
    belongs_to :card, :class_name => 'Card'
end

#app/models/deck.rb
Class Deck < ActiveRecord::Base
    has_many :card_positions, :class_name => 'DeckPosition'
    has_many :cards, :class_name => 'Card', :through => :card_positions

    accepts_nested_attributes_for :cards
end

#app/models/card.rb
Class Card < ActiveRecord::Base 
    has_many :deck_positions, :class_name => 'DeckPosition'
    has_many :decks, :class_name => 'Deck', :through => :deck_positions
end

"" :

deck_positions table
id | deck_id | card_id | row_order_position | created_at | updated_at

, , , ,


, , ? Deck ; , , ?


, Ajax, , , , , ? , , , , ? , ?

+1

All Articles