如何使用页面上的按钮更改页面上显示的记录 (RoR)

发布于 2024-09-18 09:38:13 字数 376 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2024-09-25 09:38:13

您的控件可以轻松地将一些信息附加到查询字符串中。就像这个表单一样:

<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>

当用户单击某个按钮时,浏览器将所有表单字段发送到服务器端。在服务器端的操作中,您可以检查 params 数组的 show 属性。像这样:

@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

在您看来,只需像往常一样输出 @report 变量。


您还可以为该过滤创建一个新路由,可能是 /reports/:name,并避免使用表单。在这种情况下,您只需创建一些指向正确过滤器的链接(/reports/daily/reports/weekly 等)。在服务器端,您需要检查 reports[:name] 以获得适当的值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文