Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How do I pass array of parameters through Get method in rails? Currently my URL loocs like this:

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[]=1027 part.

Request is being sent with javascript window.open method. Is there any workaround to send not ajax Post request.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

To send an array of ids to rails in a GET request simply name your variable with square brackets at the end.

//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:

?channel_id=2&product_ids[]=6900&product_ids[]=6901

url encoded it will actually be more like this:

?channel_id=2&product_ids%5B%5D=6900&product_ids%5B%5D=6901

Rails will separate this back out for you.

Parameters: {"channel_id"=>"2", "product_ids"=>["6900", "6901"]}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...