实训的技术体系无法删除最后一个

dev_video
cxt 5 years ago
parent 58ed01395e
commit 4f19a45b15

@ -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

@ -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

@ -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}) %>
</td>
<td>
<%= 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}") %>
</td>
<td class="shixun-setting-image">
<% imageExists = File.exist?(disk_filename("Shixun",shixun.id)) %>

@ -0,0 +1 @@
$.notify({ message: '更新成功' });

@ -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

Loading…
Cancel
Save