如何在 Rails 中按日期范围进行搜索?

发布于 2024-10-15 00:12:27 字数 399 浏览 0 评论 0原文

我编写了一个 Rails 控制器函数,用于查询 MySQL 数据库,查找在特定日期范围内添加的条目。例如,类似于以下内容:

    @foos = FooTableModel.where(created > ? AND created <= ?', DateTime.new(2011, 1, 26), DateTime.new(2011, 1, 29))

此查询非常适合我的目的。然而,我想做的是允许用户设置他们想要的日期范围,而不是明确设置它。我对 erb 文件进行了一些研究,并且在访问数据方面取得了巨大成功,我只对更改我要求显示的数据感兴趣。

有没有与我想要实现的目标类似的示例?我认为这是 Rails 开发人员希望让他们的用户能够做的一件相当普遍的事情,但我在线索方面运气不佳。

I've written a Rails controller function that queries a MySQL database looking for entries that were added within a certain date range. Something like the following, for instance:

    @foos = FooTableModel.where(created > ? AND created <= ?', DateTime.new(2011, 1, 26), DateTime.new(2011, 1, 29))

This query works great for my purposes. What I would like to do, however, is to allow the user to set their desired date range rather than have it explicitly set. I've poked around with erb files somewhat and I've had great success accessing the data, I'm exclusively interested in altering what data I call for to display.

Are there any examples that are similar to what I'm trying to achieve? I assume this is a fairly common thing Rails developers would like to enable their users to do but I haven't had much luck with leads.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一场春暖 2024-10-22 00:12:27

你的表单代码在哪里?

这是一个标准的 REST 表单:

<% form_tag(foo_path) do %>
   <% select_date :start_date %>
   <% select_date :end_date %>
   <%= submit_tag "Submit", :disable_with => "Submitting..." %>
<% end %>

然后在您的控制器中,您可以使用参数按这些日期进行搜索。

start_date = params[:start_date]
end_date = params[:end_date]

请记住 foo_path 需要是静态路由。如果不是,那么您需要传递一个哈希 {:controller =>; 'foo', :action =>; "find"}

在你的控制器中你可以执行如下操作:

def index
   if params[:start_date] && params[:end_date]
       start_date = params[:start_date]
       end_date = params[:end_date]
       @foos = Foo.find(:all, :conditions => ['created_at BETWEEN ? AND ?,' start_date, end_date
   end
   responds_to do |format|
       format.html
   end  
end 

Where is your form code?

Here is a standard REST form:

<% form_tag(foo_path) do %>
   <% select_date :start_date %>
   <% select_date :end_date %>
   <%= submit_tag "Submit", :disable_with => "Submitting..." %>
<% end %>

Then in your controller you can use the parameters to search by those dates.

start_date = params[:start_date]
end_date = params[:end_date]

Keep in mind foo_path needs to be restful route. If it is not then you need to pass a hash {:controller => 'foo', :action => "find"}

In your controller you can do something like this:

def index
   if params[:start_date] && params[:end_date]
       start_date = params[:start_date]
       end_date = params[:end_date]
       @foos = Foo.find(:all, :conditions => ['created_at BETWEEN ? AND ?,' start_date, end_date
   end
   responds_to do |format|
       format.html
   end  
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文