Consider the following rails link:
考虑以下rails链接:
search_path(:query => params[:query], type: params[:type], sort: params[:sort])
There is a lot of duplication here. Is it possible to define these parameters in an array and they pass into the link? Eg.
这里有很多重复。是否可以在数组中定义这些参数并将它们传递到链接中?例如。
params: [:query, :type, :sort] # -> pass each into the link like "key: value"
1 个解决方案
#1
2
I can't think of how you could do it exactly passing it as an array like you show, however you could do something like:
我想不出你怎么能把它完全传递给像你所展示的数组一样,但你可以这样做:
search_path(params.slice(:query, :type, :sort))
This will give you the same hash you're passing in. In my opinion, it's a little cleaner.
这将为您提供与您传入的相同的哈希值。在我看来,它更清晰一些。
parameters = ActionController::Parameters.new(query: 'query', type: 'type', sort: 'sort', other: 'other')
=> {"query"=>"query", "type"=>"type", "sort"=>"sort", "other"=>"other"}
parameters.slice(:query, :type, :sort)
=> {"query"=>"query", "type"=>"type", "sort"=>"sort"}
#1
2
I can't think of how you could do it exactly passing it as an array like you show, however you could do something like:
我想不出你怎么能把它完全传递给像你所展示的数组一样,但你可以这样做:
search_path(params.slice(:query, :type, :sort))
This will give you the same hash you're passing in. In my opinion, it's a little cleaner.
这将为您提供与您传入的相同的哈希值。在我看来,它更清晰一些。
parameters = ActionController::Parameters.new(query: 'query', type: 'type', sort: 'sort', other: 'other')
=> {"query"=>"query", "type"=>"type", "sort"=>"sort", "other"=>"other"}
parameters.slice(:query, :type, :sort)
=> {"query"=>"query", "type"=>"type", "sort"=>"sort"}