diff --git a/app/assets/javascripts/admins/shixun_settings/index.js b/app/assets/javascripts/admins/shixun_settings/index.js index ece7b3233..cd3445c62 100644 --- a/app/assets/javascripts/admins/shixun_settings/index.js +++ b/app/assets/javascripts/admins/shixun_settings/index.js @@ -30,9 +30,23 @@ $(document).on('turbolinks:load', function() { type: "PUT", dataType:'script', data: json - }) + }); }); + $(".shixun-settings-list-container").on("change", '.repertoire-setting-form', function () { + var s_id = $(this).attr("data-id"); + var s_value = $(this).val(); + var s_name = $(this).attr("name"); + var json = {}; + json[s_name] = s_value; + $.ajax({ + url: "/admins/shixun_settings/" + s_id + "/update_tag_repertoires", + type: "POST", + dataType:'script', + data: json + }); + }); + $("select#settings-tag-choosed").select2({ placeholder: "请选择分类", allowClear: true diff --git a/app/controllers/admins/shixun_settings_controller.rb b/app/controllers/admins/shixun_settings_controller.rb index 2f139279b..23278c3a9 100644 --- a/app/controllers/admins/shixun_settings_controller.rb +++ b/app/controllers/admins/shixun_settings_controller.rb @@ -47,21 +47,23 @@ class Admins::ShixunSettingsController < Admins::BaseController @shixun = Shixun.find_by(id:params[:id]) @page_no = params[:page_no] || "1" @shixun_tags = TagRepertoire.order("name asc").pluck(:name,:id) - tag_ids = params[:tag_repertoires] - if tag_ids.present? - @shixun&.shixun_tag_repertoires.delete_all - tag_repertoire_ids = @shixun&.tag_repertoires&.pluck(:id) - tag_ids.each do |id| - unless tag_repertoire_ids.include?(id) - tag_repertoire = @shixun.shixun_tag_repertoires.new(shixun_id:@shixun.id,tag_repertoire_id:id) - tag_repertoire.save! - end - end - else - unless @shixun.update_attributes(setting_params) - redirect_to admins_shixun_settings_path - flash[:danger] = "更新失败" - end + unless @shixun.update_attributes(setting_params) + redirect_to admins_shixun_settings_path + flash[:danger] = "更新失败" + end + end + + def update_tag_repertoires + shixun = Shixun.find_by(id:params[:id]) + tag_repertoire_ids = params[:tag_repertoires] || [] + tag_ids = tag_repertoire_ids.reject(&:blank?).map(&:to_i) + old_tag_ids = shixun.shixun_tag_repertoires.pluck(:tag_repertoire_id) + new_ids = tag_ids - old_tag_ids + delete_ids = old_tag_ids - tag_ids + tag_params = new_ids.map{|sub| {tag_repertoire_id: sub}} + ActiveRecord::Base.transaction do + shixun.shixun_tag_repertoires.where(tag_repertoire_id: delete_ids).destroy_all + shixun.shixun_tag_repertoires.create!(tag_params) end end @@ -133,6 +135,6 @@ class Admins::ShixunSettingsController < Admins::BaseController def setting_params params.permit(:use_scope,:excute_time,:close,:status,:can_copy,:webssh,:hidden,:homepage_show,:task_pass, - :code_hidden,:vip,:page_no,:id, :is_wechat_support, tag_repertoires:[]) + :code_hidden,:vip,:page_no,:id, :is_wechat_support) end end diff --git a/app/views/admins/shixun_settings/shared/_td.html.erb b/app/views/admins/shixun_settings/shared/_td.html.erb index a77cc40fa..507d15276 100644 --- a/app/views/admins/shixun_settings/shared/_td.html.erb +++ b/app/views/admins/shixun_settings/shared/_td.html.erb @@ -13,7 +13,7 @@ <%= select_tag(:use_scope, options_for_select(status_options,shixun.use_scope),class:"form-control shixun-setting-form",data:{id:shixun.id}) %> - <%= select_tag(:tag_repertoires, options_for_select(@shixun_tags,shixun.tag_repertoires.pluck(:id)),multiple:true,class:"form-control shixun-setting-form",data:{id:shixun.id},id:"tags-chosen-#{shixun.id}") %> + <%= select_tag(:tag_repertoires, options_for_select(@shixun_tags,shixun.tag_repertoires.pluck(:id)),multiple:true,class:"form-control repertoire-setting-form",data:{id:shixun.id},id:"tags-chosen-#{shixun.id}") %> <% imageExists = File.exist?(disk_filename("Shixun",shixun.id)) %> diff --git a/app/views/admins/shixun_settings/update_tag_repertoires.js.erb b/app/views/admins/shixun_settings/update_tag_repertoires.js.erb new file mode 100644 index 000000000..c95a41ec3 --- /dev/null +++ b/app/views/admins/shixun_settings/update_tag_repertoires.js.erb @@ -0,0 +1 @@ +$.notify({ message: '更新成功' }); \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index df5757b36..80371b181 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1190,7 +1190,9 @@ Rails.application.routes.draw do end end resources :shixuns, only: [:index,:destroy] - resources :shixun_settings, only: [:index,:update] + resources :shixun_settings, only: [:index,:update] do + post :update_tag_repertoires, on: :member + end resources :shixun_feedback_messages, only: [:index] resources :shixun_recycles, only: [:index, :destroy] do post :resume, on: :member