0

I have an array of objects on my frontend that I want to pass to my rails controller.

The array of object I'm passing to my controller looks like this:

[ 
  {id: 1, position: 1}, 
  {id: 2, position: 2}
]

In my controller, I'm trying to do something like this:

def map
  params[:items].each do |item|
    todo = @user.todos.new(
      item_id: item.id,
      item_position: item.position
    )
    todo.save!
  end
end

I tried JSON.stringify on my array and then sending it to my controller but everything is a string so a lot of my methods are not working.

What is the correct way to pass in an array of objects to a rails controller?

2
  • 1
    Well of course you are receiving a string, that's what JSON.stringify does. Did you try to "unstringify" in your controller? Also: stackoverflow.com/questions/38175383/… Commented Jan 17, 2019 at 22:29
  • After I JSON.parse in rails, I'm getting an error undefined method 'id' for {:id=>1}:Hash Commented Jan 17, 2019 at 23:10

1 Answer 1

1

If params[:items] is a string ("[{:id=>1, :position=>1}, {:id=>2, :position=>2}]"), then you can use eval(). That will return an array.

Try in your rails console:

pry(main)> eval("[{:id=>1, :position=>1}, {:id=>2, :position=>2}]")
=> [{:id=>1, :position=>1}, {:id=>2, :position=>2}]

So, in your case this should do the trick:

def map
  arr = eval(params[:items])
  arr.each do |item|
    todo = @user.todos.new(
      item_id: item[:id]
    )
    todo.save!
  end
end

Hope it helps.

EDIT: It is perhaps 🤪NOT the most secure thing to do to call eval on a string that has been submitted from outside (see my "mea culpa" comment below").

So I played around with this problem and the only way I can come up with would be to encode the array in the front end and decode it in your controller. It can still rais an error if the value is NOT an array of hashes so you should have an error handler in your controller...

in JS:

let array = [{id: 1, position: 1}, {id: 2, position: 2}]
let yourParamToSendOff = escape(JSON.stringify(array))

And on your back-end:

JSON.parse(CGI.unescape("%5B%7B%22id%22%3A1%2C%22position%22%3A1%7D%2C%7B%22id%22%3A2%2C%22position%22%3A2%7D%5D"))
=> [{"id"=>1, "position"=>1}, {"id"=>2, "position"=>2}]

Sorry about my first answer....

Sign up to request clarification or add additional context in comments.

6 Comments

I'm getting an error: undefined method 'id' for {:id=>1}:Hash
I modified my answer. You used item.id. It should be item[:id]. Sorry, I did not spot it at first glance.
You're proposing that someone eval a string that came from the outside world?
😒@muistooshort You are totally right. Now I need to downvote my own answer. What can I say? It's getting late and I totally slipped... EDIT: tried to downvote but can't do it. Feel free 😉
There's always JSON.parse on the Ruby side if the client is using JSON.stringify in JavaScript. Correcting yourself is always an acceptable alternative to downvotes and answer deletion (as you just did while I was writing this comment).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.