だから、jquerysをソートしてネストされたフォームフィールドをソートしています。ソートされたときに送信するコントローラーメソッドは次のとおりです。
def sort_questions
params[:questions_attributes].to_a.each_with_index do |id, index|
question = Question.find(id)
question.position = index + 1
question.save(:validate => false)
end
render :nothing => true
end
Chromeのインスペクタを見て、渡されるパラメータは次のとおりです。
"questions_attributes"=>{"1"=>{"content"=>"Where did you grow up?", "position"=>"", "_destroy"=>"false", "id"=>"2"}, "0"=>{"content"=>"What are you doing?", "position"=>"", "_destroy"=>"false", "id"=>"3"}}
以下は、呼び出されているjqueryソート可能な関数です:
$('#questions').sortable({
items:'.fields',
placeholdet: true,
axis:'y',
update: function() {
$.post("/templates/#{@template.id}/sort_questions?_method=post&" + $('.edit_template').serialize());
}
});
位置属性は保存されていません。 sort_questionsメソッドのさまざまなバリエーションを何度も何度も試してみました。
どんな助けも素晴らしいだろう。ありがとう!
完全なパラメータは次のとおりです。
"template"=>{"name"=>"Long Term Volunteer Opportunity", "description"=>"This template will be for opportunities that are for long term missionaries.", "default"=>"1", "questions_attributes"=>{"0"=>{"content"=>"What are you doing?", "position"=>"", "_destroy"=>"false", "id"=>"3"}, "1"=>{"content"=>"Where did you grow up?", "position"=>"", "_destroy"=>"false", "id"=>"2"}}}