Store the selected talks as associated records
This commit is contained in:
parent
50c821af3a
commit
52cd3716c1
|
@ -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 ) {
|
||||||
|
|
|
@ -26,6 +26,9 @@ class TalkPreferencesController < ApplicationController
|
||||||
def update
|
def update
|
||||||
@talk_preference = TalkPreference.find params[:id]
|
@talk_preference = TalkPreference.find params[:id]
|
||||||
|
|
||||||
|
@talk_preference.transaction do
|
||||||
|
@talk_preference.selected_talks.destroy_all
|
||||||
|
|
||||||
if @talk_preference.update talk_preference_params
|
if @talk_preference.update talk_preference_params
|
||||||
render json: {
|
render json: {
|
||||||
update_url: talk_preference_url(@talk_preference),
|
update_url: talk_preference_url(@talk_preference),
|
||||||
|
@ -33,13 +36,15 @@ class TalkPreferencesController < ApplicationController
|
||||||
uid: @talk_preference.id
|
uid: @talk_preference.id
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
render status: :unprocessable_entity
|
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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue