简体   繁体   English

Rails:用params排序查询?

[英]Rails: Sorting a query by params?

I am using running a simple find all and paginating with willpaginate, but I'd also like to have the query sorted by the user. 我正在使用运行一个简单的查找全部并使用willpaginate进行分页,但我也希望将查询按用户排序。 The first solution that came to mind was just use a params[:sort] 想到的第一个解决方案就是使用params [:sort]

http://localhost:3000/posts/?sort=created_at+DESC

@posts = Post.paginate :page => params[:page], :order => params[:sort]

But the problem with his approach is that the query is defaulting as sorting by ID and I want it to be created_at. 但他的方法的问题是查询默认为按ID排序,我希望它是created_at。

Is this a safe approach to sorting and is there a way to default to created_at? 这是一种安全的排序方法,有没有办法默认为created_at?

I'd use a named scope for providing the default order (available since Rails 2.1). 我使用命名范围来提供默认顺序(自Rails 2.1起可用)。

You'd add the scope in your Post model: 您将在Post模型中添加范围:

named_scope :ordered, lambda {|*args| {:order => (args.first || 'created_at DESC')} }

Then you can call: 然后你可以打电话:

@posts = Post.ordered.paginate :page => params[:page]

The example above will use the default order from the named_scope ( created_at DESC ), but you can also provide a different one: 上面的示例将使用named_scopecreated_at DESC )中的默认顺序,但您也可以提供另一个:

@posts = Post.ordered('title ASC').paginate :page => params[:page]

You could use that with Romulo's suggestion: 你可以在Romulo的建议中使用它:

sort_params = { "by_date" => "created_at", "by_name" => "name" }
@posts = Post.ordered(sort_params[params[:sort]]).paginate :page => params[:page]

If params[:sort] isn't found in sort_params and returns nil then named_scope will fall back to using the default order. 如果在sort_params找不到params[:sort]并返回nil那么named_scope将回退到使用默认顺序。

Railscasts has some great info on named_scopes. Railscasts在named_scopes上有一些很棒的信息。

In general, the way to supply default values for Hash and Hash-like objects is to use fetch : 通常,为Hash和Hash类对象提供默认值的方法是使用fetch

params.fetch(:sort){ :created_at }

A lot of people just use || 很多人只是使用|| though: 虽然:

params[:sort] || :created_at

I prefer fetch myself as being more explicit, plus it doesn't break when false is a legitimate value. 我宁愿fetch自己为更加明确,加上当它不破false是一个合法的值。

The Ruby idiom to set a default would be: 设置默认值的Ruby习语是:

@posts = Post.paginate :page => params[:page], :order => params[:sort] || "created_at"

But the approach isn't safe. 但这种方法并不安全。 The paginate method will not bother with a parameter like "created_at; DROP DATABASE mydatabase;" paginate方法不会打扰像"created_at; DROP DATABASE mydatabase;"这样的参数"created_at; DROP DATABASE mydatabase;" . Instead, you could use a dictionary of valid sort parameters (untested): 相反,您可以使用有效排序参数的字典(未经测试):

sort_params = { "by_date" => "created_at", "by_name" => "name" }

@posts = Post.paginate :page => params[:page], :order => sort_params[params[:sort] || "by_date"]

So that the URI becomes: 这样URI变为:

http://localhost:3000/posts/?sort=by_date

I prefer this idiom: 我更喜欢这个成语:

@posts = Post.paginate :page=>page, :order=>order
...

def page
  params[:page] || 1
end

def order
  params[:order] || 'created_at ASC'
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM