正在尝试实现rankmodel宝石提供的拖放特性,似乎做得不对,因为每次单击链接时,show mwthod都会引发错误
找不到ID =“ row_order_id”的评估,
注意:这些项目已经在有序列表中,我只需要添加拖放功能即可。
item.rb
class Item < ApplicationRecord
belongs_to :assessment
include RankedModel
ranks :row_order, with_same: :assessment_id
validates :title, presence: true
end
routes.rb
resources :assessments do
post :update_row_order, on: :collection
end
assessments_controller
class AssessmentsController < ApplicationController
# Display all assessments
def index
@assessments = current_user.visible_assessments
end
def show
@assessment = Assessment.find(params[:id])
@items = @assessment.items.rank(:row_order).all
end
def create
@assessment = Assessment.new(assessment_params)
if @assessment.save
render json: @assessment, status: :created
else
render json: @assessment.errors, status: :unprocessable_entity
end
end
def update_row_order
@item = Item.find(params[:item])
@item.row_order_position = params[:item]
@item.save
render nothing: true
end
private
def assessment_params
params.require(:assessment).permit(:name)
end
end
javascript
$.ajax({
type: 'POST',
url: '/assessments/update_row_order',
dataType: 'json',
data: { item: { row_order_position: 0 } }
});