joseramonc
joseramonc

Reputation: 1931

how to send rails parameters with square brackets route

What's the equivalent to new_product_path(:param_key => 'param_value') with the square brackets routes like [:new, :products, :param_key => 'param_value']? This doesn't work. How do I send parameters with the square brackets?

Upvotes: 0

Views: 1299

Answers (1)

Richard Peck
Richard Peck

Reputation: 76784

Square brackets represent an array (as in most other languages) - which means if you're looking to pass params "in square brackets", it will need to be an array of values


Params

You have to remember the Rails params object is really just a hash which is set when you submit your HTML form data. This means if you're trying to read certain params, the way to access them is through the hash: params{ your: "attributes", here: ["1", "2", "3"]}

This means if you're trying to set / access params "in square brackets", you'll need to create an array of values, like this:

<%= form_for @var do |f| %>
    <%= f.text_field :tags %>
    <%= f.text_field :tags %>
    <%= f.text_field :tags %>
<% end %>

params { tags: ["1", "2", "3"]}

Upvotes: 2

Related Questions