为SearchLogic添加一个remote_form_for方法。

本文介绍了一种简化Rails应用中搜索表单的方法。通过修改form_for和remote_form_for方法,可以自动将搜索请求设置为GET方法,并调整参数作用域。这种方法提高了表单处理效率。
稍微看了一下,好像挺简单,只是处理了一下参数。
    # Automatically makes the form method :get if a Searchlogic::Search and sets
# the params scope to :search
def form_for(*args, &block)
if search_obj = args.find { |arg| arg.is_a?(Searchlogic::Search) }
options = args.extract_options!
options[:html] ||= {}
options[:html][:method] ||= :get
options[:url] ||= url_for
args.unshift(:search) if args.first == search_obj
args << options
end
super
end

#照抄着上面写了个remote_form_for
def remote_form_for(*args, &block)
if search_obj = args.find { |arg| arg.is_a?(Searchlogic::Search) }
options = args.extract_options!
options[:html] ||= {}
options[:method] ||= :get #改了一下这里就可以正常使用了。
options[:url] ||= url_for
args.unshift(:search) if args.first == search_obj
args << options
end
super
end
import java.util.Scanner; import java.util.regex.Pattern; import java.util.ArrayList; import java.util.Comparator; import java.util.List; class Person { private int id; private String name; private String age; private String sex; private String telNum; private String address; public Person(String name, String age, String sex, String telNum, String address) { this.name = name; this.age = age; this.sex = sex; this.telNum = telNum; this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getTelNum() { return telNum; } public void setTelNum(String telNum) { this.telNum = telNum; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return String.format("序号 %d# 姓名 %s 年龄%s 性别%s 电话号码 %s 住址%s", id, name, age, sex, telNum, address); } } class TelNoteRegex { private Scanner scanner; public TelNoteRegex() { this.scanner = new Scanner(System.in); } public int menuRegex(int min, int max) { while (true) { try { System.out.printf("请输入正确的数字,最小是:%d最大是:%d\n", min, max); String input = scanner.nextLine(); int choice = Integer.parseInt(input); if (choice >= min && choice <= max) { return choice; } else { System.out.println("输入错误,请重新输入!"); } } catch (NumberFormatException e) { System.out.println("请输入有效的数字!"); } } } public String nameRegex() { while (true) { System.out.print("输入姓名,1-10位字母: "); String name = scanner.nextLine().trim(); if (Pattern.matches("^[a-zA-Z]{1,10}$", name)) { return name; } else { System.out.println("输入姓名错误,请检查"); } } } public String ageRegex() { while (true) { System.out.print("输入年龄(1-120): "); String age = scanner.nextLine().trim(); try { int ageNum = Integer.parseInt(age); if (ageNum >= 1 && ageNum <= 120) { return age; } else { System.out.println("年龄范围错误!"); } } catch (NumberFormatException e) { System.out.println("请输入有效的数字!"); } } } public String sexRegex() { while (true) { System.out.print("输入性别(M-男, F-女): "); String sex = scanner.nextLine().trim().toUpperCase(); if (sex.equals("M") || sex.equals("F")) { return sex; } else { System.out.println("性别输入错误!"); } } } public String telNumRegex() { while (true) { System.out.print("输入电话号码(1-15位数字): "); String telNum = scanner.nextLine().trim(); if (Pattern.matches("^\\d{1,15}$", telNum)) { return telNum; } else { System.out.println("电话号码格式错误!"); } } } public String addressRegex() { while (true) { System.out.print("输入地址(1-50位字符): "); String address = scanner.nextLine().trim(); if (address.length() >= 1 && address.length() <= 50) { return address; } else { System.out.println("地址长度错误!"); } } } public int getIdInput(String prompt) { while (true) { try { System.out.print(prompt); String input = scanner.nextLine(); return Integer.parseInt(input); } catch (NumberFormatException e) { System.out.println("请输入有效的数字ID!"); } } } } class Operate { private List<Person> persons; private int nextId; private TelNoteRegex regex; public Operate() { this.persons = new ArrayList<>(); this.nextId = 1; this.regex = new TelNoteRegex(); } public void addLogic() { System.out.println("\n=== 添加新联系人 ==="); String name = regex.nameRegex(); String age = regex.ageRegex(); String sex = regex.sexRegex(); String telNum = regex.telNumRegex(); String address = regex.addressRegex(); Person person = new Person(name, age, sex, telNum, address); person.setId(nextId++); persons.add(person); System.out.println("✓ 联系人添加成功!"); showAll(); } public void searchLogic() { boolean inSearchMenu = true; while (inSearchMenu) { System.out.println("\n=== 查找联系人 ==="); System.out.println("***1按姓名查找**2按年龄查找**3按性别查找**4按号码查找**5按住址查找**6查看全记录**7返回上一级***"); int choice = regex.menuRegex(1, 7); switch (choice) { case 1: searchByName(); break; case 2: searchByAge(); break; case 3: searchBySex(); break; case 4: searchByTelNum(); break; case 5: searchByAddress(); break; case 6: showAll(); break; case 7: inSearchMenu = false; break; } } } private void searchByName() { System.out.print("输入姓名: "); String name = regex.nameRegex(); boolean found = false; for (Person person : persons) { if (person.getName().equalsIgnoreCase(name)) { System.out.println(person); found = true; } } if (!found) { System.out.println("未找到姓名为 " + name + " 的联系人"); } } private void searchByAge() { System.out.print("输入年龄: "); String age = regex.ageRegex(); boolean found = false; for (Person person : persons) { if (person.getAge().equals(age)) { System.out.println(person); found = true; } } if (!found) { System.out.println("未找到年龄为 " + age + " 的联系人"); } } private void searchBySex() { System.out.print("输入性别: "); String sex = regex.sexRegex(); boolean found = false; for (Person person : persons) { if (person.getSex().equalsIgnoreCase(sex)) { System.out.println(person); found = true; } } if (!found) { System.out.println("未找到性别为 " + sex + " 的联系人"); } } private void searchByTelNum() { System.out.print("输入电话号码: "); String telNum = regex.telNumRegex(); boolean found = false; for (Person person : persons) { if (person.getTelNum().equals(telNum)) { System.out.println(person); found = true; } } if (!found) { System.out.println("未找到电话号码为 " + telNum + " 的联系人"); } } private void searchByAddress() { System.out.print("输入地址关键词: "); String keyword = regex.addressRegex(); boolean found = false; for (Person person : persons) { if (person.getAddress().toLowerCase().contains(keyword.toLowerCase())) { System.out.println(person); found = true; } } if (!found) { System.out.println("未找到地址包含 " + keyword + " 的联系人"); } } public void modifyLogic() { if (persons.isEmpty()) { System.out.println("电话簿为空,无法修改"); return; } showAll(); int id = regex.getIdInput("请输入要修改的联系人ID: "); Person target = findPersonById(id); if (target != null) { modifyPerson(target); } else { System.out.println("未找到ID为 " + id + " 的联系人"); } } private void modifyPerson(Person person) { System.out.println("当前信息: " + person); System.out.println("请选择修改项目: 1.姓名 2.年龄 3.性别 4.电话 5.地址 6.全部修改"); int choice = regex.menuRegex(1, 6); switch (choice) { case 1: person.setName(regex.nameRegex()); break; case 2: person.setAge(regex.ageRegex()); break; case 3: person.setSex(regex.sexRegex()); break; case 4: person.setTelNum(regex.telNumRegex()); break; case 5: person.setAddress(regex.addressRegex()); break; case 6: person.setName(regex.nameRegex()); person.setAge(regex.ageRegex()); person.setSex(regex.sexRegex()); person.setTelNum(regex.telNumRegex()); person.setAddress(regex.addressRegex()); break; } System.out.println("✓ 修改成功!新信息: " + person); } public void deleteLogic() { boolean inDeleteMenu = true; while (inDeleteMenu) { System.out.println("\n=== 删除联系人 ==="); System.out.println("***1查看全记录**2删除指定记录**3删除全部记录**4返回上一级***"); int choice = regex.menuRegex(1, 4); switch (choice) { case 1: showAll(); break; case 2: deleteById(); break; case 3: deleteAll(); break; case 4: inDeleteMenu = false; break; } } } private void deleteById() { if (persons.isEmpty()) { System.out.println("电话簿为空,无法删除"); return; } showAll(); int id = regex.getIdInput("请输入要删除的联系人ID: "); Person target = findPersonById(id); if (target != null) { persons.remove(target); reorganizeIds(); System.out.println("✓ 删除成功!"); showAll(); } else { System.out.println("未找到ID为 " + id + " 的联系人"); } } private void deleteAll() { if (persons.isEmpty()) { System.out.println("电话簿已为空"); return; } persons.clear(); nextId = 1; System.out.println("✓ 电话簿内容已清空"); } public void orderLogic() { boolean inOrderMenu = true; while (inOrderMenu) { System.out.println("\n=== 排序联系人 ==="); System.out.println("***1按姓名排序**2按年龄排序**3按性别排序**4返回上一级***"); int choice = regex.menuRegex(1, 4); switch (choice) { case 1: orderByName(); break; case 2: orderByAge(); break; case 3: orderBySex(); break; case 4: inOrderMenu = false; break; } } } private void orderByName() { persons.sort(Comparator.comparing((Person p) -> p.getName())); System.out.println("✓ 按姓名排序完成"); showAll(); } private void orderByAge() { persons.sort(Comparator.comparing( p-> Integer.parseInt(p.getAge()))); System.out.println("✓ 按年龄排序完成"); showAll(); } private void orderBySex() { persons.sort(Comparator.comparing((Person p) -> p.getSex())); System.out.println("✓ 按性别排序完成"); showAll(); } public void showAll() { if (persons.isEmpty()) { System.out.println("电话簿为空"); return; } System.out.println("\n=== 所有联系人 ==="); for (Person person : persons) { System.out.println(person); } } private Person findPersonById(int id) { for (Person person : persons) { if (person.getId() == id) { return person; } } return null; } private void reorganizeIds() { nextId = 1; for (Person person : persons) { person.setId(nextId++); } } } class Menu { private Operate operate; public Menu() { this.operate = new Operate(); } public void mainMenu() { TelNoteRegex regex = new TelNoteRegex(); boolean running = true; System.out.println("=== 欢迎使用电话簿程序 ==="); while (running) { System.out.println("\n" + "*".repeat(50)); System.out.println("***1添加记录***2查找记录***3修改记录***4删除记录***5排序记录***6退出系统***"); System.out.println("*".repeat(50)); int choice = regex.menuRegex(1, 6); switch (choice) { case 1: addMenu(); break; // 进入添加菜单 case 2: searchMenu(); break; // 进入查找菜单 case 3: modifyMenu(); break; // 进入修改菜单 case 4: deleteMenu(); break; // 进入删除菜单 case 5: orderMenu(); break; // 进入排序菜单 case 6: running = false; System.out.println("感谢使用电话簿程序,再见!"); break; } } } private void addMenu() { TelNoteRegex regex = new TelNoteRegex(); boolean inAddMenu = true; while (inAddMenu) { System.out.println("\n=== 添加联系人菜单 ==="); System.out.println("***1添加新记录**2查看全记录**3返回上一级***"); int choice = regex.menuRegex(1, 3); switch (choice) { case 1: operate.addLogic(); break; case 2: operate.showAll(); break; case 3: inAddMenu = false; break; } } } private void searchMenu() { operate.searchLogic(); } private void modifyMenu() { operate.modifyLogic(); } private void deleteMenu() { operate.deleteLogic(); } private void orderMenu() { operate.orderLogic(); } } public class App { public static void main(String[] args) { Menu menu = new Menu(); menu.mainMenu(); } }翻译
01-04
【完美复现】面向配电网韧性提升的移动储能预布局与动态调度策略【IEEE33节点】(Matlab代码实现)内容概要:本文介绍了基于IEEE33节点的配电网韧性提升方法,重点研究了移动储能系统的预布局与动态调度策略。通过Matlab代码实现,提出了一种结合预配置和动态调度的两阶段优化模型,旨在应对电网故障或极端事件时快速恢复供电能力。文中采用了多种智能优化算法(如PSO、MPSO、TACPSO、SOA、GA等)进行对比分析,验证所提策略的有效性和优越性。研究不仅关注移动储能单元的初始部署位置,还深入探讨其在故障发生后的动态路径规划与电力支援过程,从而全面提升配电网的韧性水平。; 适合人群:具备电力系统基础知识和Matlab编程能力的研究生、科研人员及从事智能电网、能源系统优化等相关领域的工程技术人员。; 使用场景及目标:①用于科研复现,特别是IEEE顶刊或SCI一区论文中关于配电网韧性、应急电源调度的研究;②支撑电力系统在灾害或故障条件下的恢复力优化设计,提升实际电网应对突发事件的能力;③为移动储能系统在智能配电网中的应用提供理论依据和技术支持。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析,重点关注目标函数建模、约束条件设置以及智能算法的实现细节。同时推荐参考文中提及的MPS预配置与动态调度上下两部分,系统掌握完整的技术路线,并可通过替换不同算法或测试系统进一步拓展研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值