Reputation: 1775
I have an rails object named app, when i send it to controller by following method then i get params[:app] as array in the controller. I have already tried a lot on stack overflow but did not found what i was looking for
assert_difference("Tagging.count", 3) do
put "/candidates/#{app.id}", {id: app.id, assigned_tags: " #{tag_one.name} #{tag_two.name} #{tag_three.name} " , app: app.to_json }
end
how can i get the app as hash in controller through params[:app] ??
Upvotes: 1
Views: 1084
Reputation: 1208
You can't send an object through params. Normally you should send the ID of that object in params and in receiving end you should lookup in the controller.
user = User.find(params[:user_id])
If you send an object as params, it'll be converted to array automatically.
Upvotes: 0
Reputation: 32933
There are two data structures you can build with the name attribute of a form field.
foo[]
will put the value into an array called foo
foo[bar]
will put the value into a hash called foo
, using the key bar
.
eg
<input type="text" name="foo[]" value="bacon">
<input type="text" name="foo[]" value="chicken">
=> params = {:foo => ["bacon", "chicken"]}
<input type="text" name="foo[bar]" value="bacon">
<input type="text" name="foo[baz]" value="chicken">
=> params = {:foo => {:bar => "bacon", :baz => "chicken"}}
These can be combined:
<input type="text" name="foo[bar][]" value="bacon">
<input type="text" name="foo[baz][]" value="chicken">
=> params = {:foo => {:bar => ["bacon"], :baz => ["chicken"]}}
<input type="text" name="foo[][bar]" value="bacon">
<input type="text" name="foo[][baz]" value="chicken">
=> params = {:foo => [{:bar => "bacon", :baz => "chicken"}]}
Upvotes: 2