如何使用页面上的按钮(RoR)更改页面上显示的记录 [英] How do I change the records being displayed on the page with buttons on the page (RoR)
问题描述
我有一个rails应用程序,显示基于员工日常生产的统计数据。目前我的页面显示所有记录。
我知道如何显示各种不同的组合,例如在两个日期之间进行的记录等...但是我真正想做的是使它成为单页(说索引页)有3个控件允许它在日常统计记录,每周统计记录和统计记录的自定义日期约束之间切换(例如从日期xx / xx / 2009到日期xx / xx / 2010)。我一直在搜索一段时间来试图弄清楚这一点,但是我显然缺少一些东西,因为我找不到遇到同样问题的任何人。
如果这样做太难了,其他的 - 大多数简单的方法我可以看到这样做是为每个视图创建一个页面,但是仍然留下一个问题,我如何设置控件来选择自定义日期约束。 / p>
我为我的新生提前道歉,但如果有人可以向我解释这一点,我认为这将真正增加我对rails的理解。感谢提前
您的控件可以轻松地将一些信息附加到查询字符串。像这个表单一样:
< form action =method =get>
< fieldset>
< button type =submitname =showvalue =daily>每日统计信息< / button>
< button type =submitname =showvalue =weekly>每周统计信息< / button>
< / fieldset>
< fieldset>
从< input type =textname =interval-startvalue =/>直到
< input type =textname =interval-endvalue =/>
< button type =submitname =showvalue =interval>指定间隔的统计信息< / button>
< / fieldset>
< / form>
当用户点击某个按钮时,浏览器将所有表单字段发送到服务器端。在服务器端,在您的操作中,您可以检查 params
数组的 show
属性。像这样:
@reports = case params [show]
当daily
#只是一个例子在Rails 3中使用ActiveRecord的查询界面或Arel,而不是纯查询
Report.find_by_sqlSELECT * FROM reports WHERE DATE(report_date)= DATE(NOW())
当weekly
Report.find_by_sqlSELECT * FROM reports WHERE WEEK(report_date)= WEEK(NOW())
当间隔
Report.find_by_sql...
else
#一些其他可能的值,过滤器等等
end
在您的视图中只输出
您还可以创建一个 @report
用于过滤的新路线,也许 / reports /:name
,并避免使用表单。在这种情况下,您只创建一些指向正确过滤器的链接( / reports / daily
, / reports / weekly
等)。在服务器端,您需要检查报告[:name]
以获取适当的值。
I have a rails app that shows statistics based on an employee's daily production. currently my page shows ALL records.
I know how to show various different combinations such as records made between two dates etc... but what I really would like to do is make it so that single page (say the index page) has 3 controls that allow for it to switch between daily statistic records, weekly statistic records and a custom date constraint of statistic records (such as from date xx/xx/2009 to date xx/xx/2010). I have been searching for a while to attempt to figure this out but I am obviously missing something as I cannot find anyone else who has run into the same issues.
If this is too difficult to do this way, the other - mostly easy way I can see to do this is to create a page for each view, however it still leaves a question on how I set up the control to choose the custom date constraints.
I aplogise in advance for my newbyness but if someone could explain this to me, I think it is going to really increase my understanding of rails. Thanks in advance
Your control can easily append some information to the query string. Like this form does:
<form action="" method="get">
<fieldset>
<button type="submit" name="show" value="daily">Daily stats</button>
<button type="submit" name="show" value="weekly">Weekly stats</button>
</fieldset>
<fieldset>
From <input type="text" name="interval-start" value="" /> till
<input type="text" name="interval-end" value="" />
<button type="submit" name="show" value="interval">Stats for the specified interval</button>
</fieldset>
</form>
When user clicks on some button, browser sends all form fields to the server-side. On the server-side, in your action, you can check for the show
property of the params
array. Like this:
@reports = case params["show"]
when "daily"
#Just an example. Use ActiveRecord's query interface or Arel in Rails 3, not plain queries
Report.find_by_sql "SELECT * FROM reports WHERE DATE(report_date) = DATE(NOW())"
when "weekly"
Report.find_by_sql "SELECT * FROM reports WHERE WEEK(report_date) = WEEK(NOW())"
when "interval"
Report.find_by_sql "..."
else
#Some other possible values, filters, so on
end
And in your view just output the @report
variable as usual.
You could also create a new route for that filtering, maybe /reports/:name
, and avoid using forms. In this case, you only create some links that point to the right filter (/reports/daily
, /reports/weekly
, etc). On the server-side you need to check reports[:name]
for the appropriate value.
这篇关于如何使用页面上的按钮(RoR)更改页面上显示的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!