为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
【激光质量检测】利用丝杆与步进电机的组合装置带动光源的移动,完成对光源使用切片法测量其光束质量的目的研究(Matlab代码实现)内容概要:本文研究了利用丝杆与步进电机的组合装置带动光源移动,结合切片法实现对激光光源光束质量的精确测量方法,并提供了基于Matlab的代码实现方案。该系统通过机械装置精确控制光源位置,采集不同截面的光强分布数据,进而分析光束的聚焦特性、发散角、光斑尺寸等关键质量参数,适用于高精度光学检测场景。研究重点在于硬件控制与图像处理算法的协同设计,实现了自动化、高重复性的光束质量评估流程。; 适合人群:具备一定光学基础知识和Matlab编程能力的科研人员或工程技术人员,尤其适合从事激光应用、光电检测、精密仪器开发等相关领域的研究生及研发工程师。; 使用场景及目标:①实现对连续或脉冲激光器输出光束的质量评估;②为激光加工、医疗激光、通信激光等应用场景提供可靠的光束分析手段;③通过Matlab仿真与实际控制对接,验证切片法测量方案的有效性与精度。; 阅读建议:建议读者结合机械控制原理与光学测量理论同步理解文档内容,重点关注步进电机控制逻辑与切片数据处理算法的衔接部分,实际应用时需校准装置并优化采样间距以提高测量精度。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值