ROR 请求一个操作来获取一系列项目

发布于 2024-10-19 15:26:30 字数 217 浏览 5 评论 0原文

我如何创建一个接受参数来限制搜索结果的操作?这将超出我的帖子模型

Posts_controller:

def getOneThrougTen

params[firstNum] params[secondNum]

某种类型的返回范围?

结束

How would I create an action that takes in parameters to limit search results? This would be out of my Posts model

Posts_controller:


def getOneThrougTen

params[firstNum] params[secondNum]

return range of some sort?

end

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

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

发布评论

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

评论(3

聚集的泪 2024-10-26 15:26:30

我并不完全清楚你想要实现什么,但如果它以块的形式显示搜索结果,我会建议你使用 will_paginate 宝石!

It's not entirely clear to me what you want to achieve, but if it is displaying search results in chunks, I'll advice you to use the will_paginate gem!

风吹雪碎 2024-10-26 15:26:30

如果您正在寻找分页类型代码,您应该查看 Rails 3 中 Arel 和 ActiveRecord 提供的新功能。这将允许您执行以下操作:

@posts = Post.all.limit(myLimit).offset(myOffset)

@posts = Post.where(["user_id = ?",myUserId]).limit(myLimit).offset(myOffset).order("created_at desc")

等等。非常强大的东西。

If you're looking for pagination type code, you should look at the new capabilities provided with Arel and ActiveRecord in Rails 3. That will allow you to do the following:

@posts = Post.all.limit(myLimit).offset(myOffset)

or

@posts = Post.where(["user_id = ?",myUserId]).limit(myLimit).offset(myOffset).order("created_at desc")

etc. etc. Very powerful stuff.

风情万种。 2024-10-26 15:26:30

一旦您了解了活动记录如何模仿 sql 调用的基础知识,传递参数就变得直观了。例如,这就是您如何根据特定列的范围从 Post 模型创建 @posts 集合、对结果进行排序并限制返回的数量。

您可以观看脚本/服务器输出以查看实际的 SQL,以帮助了解正在发生的情况以及为什么某些事情没有按计划进行。

@posts=Post.find(:all, :conditions=>["db_column_name between ? and ?", firstNum, secondNum],:order=>"db_column_name ASC", :limit=>10)

要实际发送并读取您的参数..
在控制器中,您可以读取视图发送的参数并将它们分配给变量。例如你可以这样做。

 def getDateRangeandPassItOn
     firstNum=params[:firstNum] 
     ...

实际上将它们从视图发送到控制器是不同的,具体取决于您是否使用完全安静的控制器(例如在 Railsscaffoldind 中生成的控制器)。如果您想要的只是将它们发送到非静态方法(例如“getDateRangeandPassItOn”),那么在您看来,您会执行类似的操作。

<% form_for ({:controller => 'name', :action => "getDateRangeandPassItOn"}) do f%>    
<%f.label :firstNum%> 
<%f.textField :firstNum%>
<%f.submit "submit"%>
<%end%> 

Once you learn the basics of how active record mimics sql calls passing the parameters becomes intuitive. For example this is how you would create a collection of @posts from your Post model based on a range from a particular column, order the results and limit the number returned.

You can watch the script/server output to see the actual SQL to help understand what is going on and why something doesn't go as planned.

@posts=Post.find(:all, :conditions=>["db_column_name between ? and ?", firstNum, secondNum],:order=>"db_column_name ASC", :limit=>10)

To actually send and read your params..
In your controller you can read params sent by the view and assign them to variables. For example you could do.

 def getDateRangeandPassItOn
     firstNum=params[:firstNum] 
     ...

To actually send them from the view to your controller is different depending on if you are using a fully restful controller such as the one generated in rails scaffoldind. If all you want is to send them into a non restful method such as "getDateRangeandPassItOn" you would do something like this in your view.

<% form_for ({:controller => 'name', :action => "getDateRangeandPassItOn"}) do f%>    
<%f.label :firstNum%> 
<%f.textField :firstNum%>
<%f.submit "submit"%>
<%end%> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文