user938363
user938363

Reputation: 10368

How to pass a large array to a Rails path?

We need to pass a large array of ids (may be a few thousands of them) to a path in rails:

rails_path(ids: large_id_array)

There is error of Request-URI Too Large popping up in development passing an array of 2700 ids. What's the size limit when passing an array to a rails path? Is there any way getting around the size limitation?

Upvotes: 0

Views: 945

Answers (1)

MrYoshiji
MrYoshiji

Reputation: 54892

You can pass as many args in the path as you want with POST params (you were trying to pass GET params):

  • GET params are visible in the URL (example: /users?is_admin=true the params[:is_admin] is equal to "true" in this case)
  • POST params are not visible in the URL (like when you click on "login" button after you've filled the username/password fields, for obvious security reasons you don't want to print the fields in the URL)

In your case, you should pass the ids as POST params. How to send POST params in a request? Just ask link_to to use POST method instead of GET:

link_to 'Click here!', rails_path(ids: large_array_of_ids), method: :post

Another way to create a POST link:

# HAML code
= form_tag rails_path(ids: large_array_of_ids) do
  = submit_tag 'Click here!'

# ERB code
<%= form_tag rails_path(ids: large_array_of_ids) do %>
  <%= submit_tag 'Click here!' %>
<% end %>

Upvotes: 4

Related Questions