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 */ /* 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( !myapi || !myapi.length ) {
/* If we do not have resource URL, post data and get resource */ /* If we do not have resource URL, post data and get resource */
$.post( halfnarpAPI, request, function( data ) { $.post( halfnarpAPI, request, function( data ) {

View File

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

View File

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