From 858f3f8b85f039b3f888e1155b2b79cc56ac1b61 Mon Sep 17 00:00:00 2001 From: chiu Date: Tue, 23 Feb 2021 23:55:50 +0800 Subject: [PATCH] fix error --- app/controllers/admin/asks_controller.rb | 34 +++++++++++++----------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/app/controllers/admin/asks_controller.rb b/app/controllers/admin/asks_controller.rb index 268caf1..0dfdb12 100644 --- a/app/controllers/admin/asks_controller.rb +++ b/app/controllers/admin/asks_controller.rb @@ -161,15 +161,19 @@ class Admin::AsksController < OrbitAdminController custom_exist_nums = [] custom_exists = text.scan(/{custom\d+}/) custom_exists.each do |custom_exist| - k = custom_exist[6...-1] + k = custom_exist[6...-1].to_i - 1 custom_exist_nums << k end customs.each do |custom| - tmp = custom_fields.keys.select{|key| custom_exist_nums.exclude? key}.collect do |k| - tp = custom.gsub(/{custom}/,"{custom#{k}}") - tp = tp.gsub(/{custom_title}/,"{custom#{k}_title}") - tp - end.join + tmp = custom_fields.keys.collect.with_index do |k,i| + if custom_exist_nums.exclude?(i) + tp = custom.gsub(/{custom}/,"{custom#{i+1}}") + tp = tp.gsub(/{custom_title}/,"{custom#{i+1}_title}") + tp + else + nil + end + end.compact.join text = text.gsub(custom,tmp) end end @@ -178,10 +182,10 @@ class Admin::AsksController < OrbitAdminController if format_text[-7...-1] == '_title' case format_text[1...-7] when /custom\d+/ - i = format_text.scan(/\d+/)[0] - k = custom_fields.keys.index(i) rescue nil + i = format_text.scan(/\d+/)[0].to_i - 1 + k = custom_fields.keys[i] rescue nil if !k.blank? - text_tp = custom_fields.values[k]['field'][I18n.locale] rescue '' + text_tp = custom_fields[k]['field'][I18n.locale] rescue '' text = text.gsub(format_text,text_tp) end when 'title' @@ -223,18 +227,18 @@ class Admin::AsksController < OrbitAdminController text = text.gsub(format_text,I18n.t('ask,no')) end when /custom\d+\|format/ - i = format_text.scan(/\d+/)[0] - k = custom_fields.keys.index(i) rescue nil + i = format_text.scan(/\d+/)[0].to_i - 1 + k = custom_fields.keys[i] rescue nil if !k.blank? - v = custom_fields.values[k] rescue {} + v = custom_fields[k] rescue {} text_tp = Admin::AsksHelper.show_on_front(k,v,@ask_question.custom_values[k],true,true) text = text.gsub(format_text,text_tp.to_s) end when /custom\d+/ - i = format_text.scan(/\d+/)[0] - k = custom_fields.keys.index(i) rescue nil + i = format_text.scan(/\d+/)[0].to_i - 1 + k = custom_fields.keys[i] rescue nil if !k.blank? - v = custom_fields.values[k] rescue {} + v = custom_fields[k] rescue {} text_tp = Admin::AsksHelper.show_on_front(k,v,@ask_question.custom_values[k],true) text = text.gsub(format_text,text_tp.to_s) end