Ruby-on-rails 通过 get in rails 传递参数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11312930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing array of parameters through get in rails
提问by Rafael Sedrakyan
How do I pass array of parameters through Get method in rails? Currently my URL loocs like this:
如何通过 Rails 中的 Get 方法传递参数数组?目前我的 URL 看起来像这样:
http://localhost:3000/jobs/1017/editing_job_suites/1017/editing_member_jobs/new?ids[]=1025&ids[]=1027
How can I pass the array with Get method but avoid ?ids[]=1025&ids[]=1027part.
如何使用 Get 方法传递数组但避免?ids[]=1025&ids[]=1027部分。
Request is being sent with javascript window.open method. Is there any workaround to send not ajax Post request.
正在使用 javascript window.open 方法发送请求。是否有任何解决方法可以发送非 ajax Post 请求。
采纳答案by DGM
No, GET can only put variables on the url itself. If you want the URL to be shorter, you have to POST. That's a limitationfeature of HTTP, not Rails.
不,GET 只能在 url 本身上放置变量。如果您希望 URL 更短,则必须 POST。这是 HTTP的一个限制特性,而不是 Rails。
回答by Homan
You should stick to using a GET request if you are not changing the state of anything, and all you want to to access a read only value.
如果您不更改任何状态,并且您想访问只读值,则应坚持使用 GET 请求。
To send an array of ids to rails in a GET request simply name your variable with square brackets at the end.
要在 GET 请求中将一组 id 发送到 rails,只需在末尾用方括号命名变量即可。
//angular snippet
$http.(method:'GET',
...
params: {'channel_id':2, 'product_ids[]': productIds}
//where productIds is an array of integers
...
)
Do not concatenate your ids as a comma separated list, just pass them individually redundantly. So in the url it would look something like this:
不要将您的 id 连接为逗号分隔的列表,只需冗余地单独传递它们。所以在 url 中它看起来像这样:
?channel_id=2&product_ids[]=6900&product_ids[]=6901
url encoded it will actually be more like this:
url 编码它实际上更像这样:
?channel_id=2&product_ids%5B%5D=6900&product_ids%5B%5D=6901
Rails will separate this back out for you.
Rails 会为你把它分开。
Parameters: {"channel_id"=>"2", "product_ids"=>["6900", "6901"]}
回答by Jason Preston
I recently wanted to do this and found a workaround that is a little less complex, and so has some complexity limitations but may also be easier to implement. Can't speak to security, etc.
我最近想这样做,并找到了一个不太复杂的解决方法,因此有一些复杂性限制,但也可能更容易实现。不能和保安说话等等。
If you pass your array values as a string with a delimiter, e.g.
如果您将数组值作为带有分隔符的字符串传递,例如
http://example.com/controller?job_ids=2342,2354,25245
Then you can take the result and parse it back to what you want:
然后您可以获取结果并将其解析回您想要的内容:
job_ids = params[:job_ids].split(',')
Then do whatever:
然后做任何事情:
job_ids.each do |job_id|
job = Job.find(job_id.to_i)
end
etc
等等
回答by akostadinov
@Homan answer is valid for using an external client (e.g curlor angular). Inside Rails test cases though, you should not use []. Here's an example:
@Homan 答案对于使用外部客户端(例如curl或 angular)有效。但是,在 Rails 测试用例中,您不应该使用[]. 下面是一个例子:
get get_test_cases_url(**path_params), params: {case_ids: ["NON-9450", "NON-12345", "NON-9361"]}
This is using new format where get_test_casesis name of route and you pass to the method all params needed to construct the URL path. Query params are a separate hash.
这是使用新格式,其中get_test_cases是路由名称,您将构建 URL 路径所需的所有参数传递给方法。查询参数是一个单独的哈希。
FYI if I use []like case_ids[], then instead of ["NON-9450", "NON-12345", "NON-9361"]I'm getting it parsed to [["NON-9450"], ["NON-12345"], ["NON-9361"]]
仅供参考,如果我使用[]like case_ids[],那么["NON-9450", "NON-12345", "NON-9361"]我将其解析为[["NON-9450"], ["NON-12345"], ["NON-9361"]]

