如何使用页面上的按钮更改页面上显示的记录 (RoR)
我有一个 Rails 应用程序,可以显示基于员工日常生产的统计数据。目前我的页面显示所有记录。
我知道如何显示各种不同的组合,例如两个日期之间的记录等...但我真正想做的是使单个页面(例如索引页)有 3 个控件,允许它在之间切换每日统计记录、每周统计记录以及统计记录的自定义日期限制(例如从日期xx/xx/2009到日期xx/xx/2010)。我已经搜索了一段时间试图解决这个问题,但我显然错过了一些东西,因为我找不到其他遇到同样问题的人。
如果这样做太困难了,我可以看到的另一种最简单的方法是为每个视图创建一个页面,但是它仍然留下一个关于如何设置控件来选择自定义日期约束的问题。
我提前为我的新手表示歉意,但如果有人可以向我解释这一点,我认为这将真正增加我对 Rails 的理解。提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的控件可以轻松地将一些信息附加到查询字符串中。就像这个表单一样:
当用户单击某个按钮时,浏览器将所有表单字段发送到服务器端。在服务器端的操作中,您可以检查
params
数组的show
属性。像这样:在您看来,只需像往常一样输出
@report
变量。您还可以为该过滤创建一个新路由,可能是
/reports/:name
,并避免使用表单。在这种情况下,您只需创建一些指向正确过滤器的链接(/reports/daily
、/reports/weekly
等)。在服务器端,您需要检查reports[:name]
以获得适当的值。Your control can easily append some information to the query string. Like this form does:
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 theparams
array. Like this: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 checkreports[:name]
for the appropriate value.