Galaxy
Galaxy

Reputation: 3400

Using ranked model with rails 4

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.

Upvotes: 4

Views: 1814

Answers (1)

Richard Peck
Richard Peck

Reputation: 76784

Several things here (I was going to write a comment, but it will be easier to read as an answer):


Does It Work Like You Want?

I looked over your code, and it seems like you're trying to just change the position of the deck, with no callback in your ajax

Is this how you want it to work? Personally, I'd make a separate model called DeckPosition, which you can use like this:

#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

I'd then include the attribute "order" in the join model:

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

This could be completely wrong, but judging from your questions, it seems like it's something you could benefit from


Nested Attributes

You seem to be passing nested attributes to your cards model, and yet you've not got any reference to this in your code? I have added the applicable code to the Deck model, in my own code; so maybe you would want to look at that?


Sorting

One of the reasons why I asked about your Ajax was that you're sending a request to your server, but don't seem to be handling it? This is important because although I've not used the ranking gem before, it seems you want to be able to sort some important aspects of your application? How do you want them to be handled?

Upvotes: 1

Related Questions