Store the selected talks as associated records

This commit is contained in:
Petko Bordjukov 2016-10-05 17:56:24 +03:00
parent 50c821af3a
commit 52cd3716c1
3 changed files with 19 additions and 12 deletions

View File

@ -41,7 +41,9 @@
}
/* Convert preferences to JSON and post them to backend */
var request = {talk_preference: {'talks': ids}};
var request = {talk_preference: {'selected_talks_attributes': ids.map(function(talk_id) {
return {"talk_id": talk_id};
})}};
if( !myapi || !myapi.length ) {
/* If we do not have resource URL, post data and get resource */
$.post( halfnarpAPI, request, function( data ) {

View File

@ -26,6 +26,9 @@ class TalkPreferencesController < ApplicationController
def update
@talk_preference = TalkPreference.find params[:id]
@talk_preference.transaction do
@talk_preference.selected_talks.destroy_all
if @talk_preference.update talk_preference_params
render json: {
update_url: talk_preference_url(@talk_preference),
@ -33,13 +36,15 @@ class TalkPreferencesController < ApplicationController
uid: @talk_preference.id
}
else
render status: :unprocessable_entity
head :unprocessable_entity
raise ActiveRecord::Rollback
end
end
end
private
def talk_preference_params
params.require(:talk_preference).permit(talks: [])
params.require(:talk_preference).permit(selected_talks_attributes: [:talk_id])
end
end

View File

@ -1,7 +1,7 @@
class TalkPreference < ActiveRecord::Base
has_many :selected_talks
self.primary_key = :unique_id
has_many :selected_talks
accepts_nested_attributes_for :selected_talks
before_create :assign_new_unique_id