为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
内容概要:本文详细介绍了一种基于Simulink的表贴式永磁同步电机(SPMSM)有限控制集模型预测电流控制(FCS-MPCC)仿真系统。通过构建PMSM数学模型、坐标变换、MPC控制器、SVPWM调制等模块,实现了对电机定子电流的高精度跟踪控制,具备快速动态响应和低稳态误差的特点。文中提供了完整的仿真建模步骤、关键参数设置、核心MATLAB函数代码及仿真结果分析,涵盖转速、电流、转矩和三相电流波形,验证了MPC控制策略在动态性能、稳态精度和抗负载扰动方面的优越性,并提出了参数自整定、加权代价函数、模型预测转矩控制和弱磁扩速等优化方向。; 适合人群:自动化、电气工程及其相关专业本科生、研究生,以及从事电机控制算法研究与仿真的工程技术人员;具备一定的电机原理、自动控制理论和Simulink仿真基础者更佳; 使用场景及目标:①用于永磁同步电机模型预测控制的教学演示、课程设计或毕业设计项目;②作为电机先进控制算法(如MPC、MPTC)的仿真验证平台;③支撑科研中对控制性能优化(如动态响应、抗干扰能力)的研究需求; 阅读建议:建议读者结合Simulink环境动手搭建模型,深入理解各模块间的信号流向与控制逻辑,重点掌握预测模型构建、代价函数设计与开关状态选择机制,并可通过修改电机参数或控制策略进行拓展实验,以增强实践与创新能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值