Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 2 - expected Hash (got Array) for param

So in HTML if I made a form like this:

<form method="post">
<input type="text" name="categories[][name]" />
<input type="text" name="categories[][name]" />
<input type="text" name="categories[][name]" />
<input type="text" name="categories[][name]" />
<input type="submit" value="submit" />
</form>

I expect params[:categories] to be

[{"name"=>"value"},{"name"=>"value"},{"name"=>"value"},{"name"=>"value"}]

But instead, rails 2 will raise a TypeError: expected Hash (got Array) for param

The error is raised here:

http://apidock.com/rails/Rack/Utils/normalize_params

Why is this not allowed or not parseable in rails? What am I missing?

I know I could index the input like so

<input type="text" name="categories[0][name]" />
<input type="text" name="categories[1][name]" />
<input type="text" name="categories[2][name]" />
<input type="text" name="categories[3][name]" />

And get a Hash. But this seems counter-productive.

like image 856
vinhboy Avatar asked Oct 21 '22 19:10

vinhboy


1 Answers

You've got to reverse the subscript order:

<input type="text" name="categories[name][]" />
like image 74
Jim Stewart Avatar answered Nov 03 '22 08:11

Jim Stewart