我不确定如何表达我的问题,如果问题的措辞不正确,非常抱歉。
我会尽量提供帮助我所需的最少信息。请让我知道要进行的编辑或要添加的内容。谢谢。
所以我已经设置了一个关系 MYSQL 数据库,其中包含 4 个表中的数据。这个程序应该是一个小规模的客户数据库查找。我有一个带有表单的 HTML 页面:一个询问客户编号的文本框和 4 个单选按钮来确定要检索的数据。我们正在使用 Ruby on Rails(我从未使用过)来访问和控制表中的数据。
4 个单选按钮:
- 客户资料
- 销售代表
- 订单
- 部分
以下是这些表所包含内容的链接: http ://www.cs.uky.edu/~paulp/CS316/tables.txt
由于输入了customerNum,前两个收音机很容易实现。对于客户数据,我设置变量以从客户表中检索值。SalesRep选项也很简单。我接受了customerNum输入,将一个变量设置为customers中的数据,将另一个变量设置为salesreps中的数据。我从以customerNum为输入的客户那里获取另一个变量作为 salesrep id 。然后使用该销售代表 ID 显示该客户的销售代表的相关数据。
第 3 和第 4 无线电选项(订单和零件)让我很难过!如果您查看 orders 表,一些customerNum有多个订单。
我的问题本质上是“我如何才能显示给定客户的所有订单(以及每个订单的相关数据)?”
因为每个customerNum可能有 1 个或多个订单,所以我不能只做前 2 台收音机的操作。我是 Ruby on Rails 的新手,不知道如何实现这一点。
稍后会发布一些代码。非常感谢您对本文的编辑或帮助实施此建议的任何建议。谢谢!
代码:
index.html.erb
<%
# index.html.erb
# First (input) screen for example
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<link rel="stylesheet" type="text/css" href="style.css">
<title>The Data Access Program</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Spartan Hardware Data Access</h1>
<form action="cs316ruby/result" method="post" >
<h3>Data requested:
<input type="text" name="data" /> </h3>
<ol>
<li><input type="radio" name="field" value="cu" id="field_cu" checked="checked" />
Customer data</li>
<li><input type="radio" name="field" value="sr" id="field_sr" />Sales rep</li>
<li><input type="radio" name="field" value="or" id="field_o" />Orders</li>
<li><input type="radio" name="field" value="p" id="field_p" />Parts</li>
</ol>
<input type="hidden" name=<%= request_forgery_protection_token.to_s %>
value=<%= form_authenticity_token %> />
<input type="submit" value="submit" />
</form>
</body>
</html>
salesrepdisplay.html.erb
<%
# salesrepisplay.html.erb.rb
# View that displays customer's sales rep data for cs316ruby program
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<link rel="stylesheet" type="text/css" href="style.css">
<title>The Data Access Program</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Spartan Hardware Stores Data Access</h1>
<% # get the customer data from the customer table
@custdata = Customer.find(@data)
# copy the Sales rep field into a variable to retrieve sales rep data
# if sales rep id = 00, the entered customer has no sales rep
@sr = @custdata.sales_rep
@srdata = Salesrep.find(@sr)
@ln = @srdata.last_name
@fn = @srdata.first_name
@rt = @srdata.rate
@cm = @srdata.commission;
@cm = sprintf("%7.2f",@cm)
%>
<h1>Sales Rep data for customer: <%=@data %></h1>
<table border=1>
<tr>
<td>ID</td>
<td>Last name</td>
<td>First name</td>
<td>Commission</td>
<td>Rate</td>
</tr>
<tr>
<td><%=@sr%></td>
<td><%=@ln%></td>
<td><%=@fn%></td>
<td><%=@cm%></td>
<td><%=@rt%></td>
</tr>
</table>
</body>
</html>
custdisplay.html.erb
<%
# custdisplay.html.erb.rb
# View that displays customer data for cs316ruby program
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<title>The Data Access Program</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Spartan Hardware Stores Data Access</h1>
<% # get the customer data from the customer table
@custdata = Customer.find(@data)
# copy each field into a variable for display
@id = @custdata.id
@ln = @custdata.last_name
@fn = @custdata.first_name
@bl = @custdata.balance;
@bl = sprintf("%7.2f",@bl) # format as currency
@cl = @custdata.credit_limit
@cl = sprintf("%7.2f",@cl) # format as currency
@sr = @custdata.sales_rep
%>
<h1>customer data for customer: <%=@data %></h1>
<table border=1>
<tr>
<td>ID</td>
<td>Last name</td>
<td>First name</td>
<td>Balance</td>
<td>Credit limit</td>
<td>Sales rep</td>
</tr>
<tr>
<td><%=@id%></td>
<td><%=@ln%></td>
<td><%=@fn%></td>
<td><%=@bl%></td>
<td><%=@cl%></td>
<td><%=@sr%></td>
</tr>
</table>
</body>
</html>
orderdisplay.html.erb
这基本上是销售代表和客户订单页面的复制和粘贴开始
<%
# orderdisplay.html.erb
# View that displays customer's order data for cs316ruby program
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<title>The Data Access Program</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Spartan Hardware Stores Data Access</h1>
<% # get the customer data from the customer table
@custdata = Customer.find(@data)
%>
<h1>Order data for customer: <%=@data %></h1>
<table border=1>
<tr>
<td>Order Number</td>
<td>Order Date</td>
<td>Cost of Order</td>
</tr>
<tr>
<td><%=@sr%></td>
<td><%=@ln%></td>
<td><%=@fn%></td>
</tr>
</table>
</body>
</html>