1

I'm using Ruby on Rails and Jbuilder gem.

I'm trying to get an attribute of a point like this:

{
"point":[33,11]
}

My model are like this

class Point < ActiveRecord::Base
attr_accessible :x, :y
end

I'm trying with this, but was not successfully, it returns the point as a string and I need it as integer.

json.point "[#{point.x},#{point.y}]"

Thanks!!!!

4
  • Have you tried point.x.to_i ? Commented Nov 17, 2012 at 1:32
  • yes, but still not ok, it returns: "[33,11]" and I need [33,11] Commented Nov 17, 2012 at 1:38
  • I see , you can try a method , that accepts point.x and point.y and returns an array [point.x,point.y] (as you know , [] represents array in Ruby). Commented Nov 17, 2012 at 1:41
  • it works! I will post the answer thanks! Commented Nov 17, 2012 at 1:50

1 Answer 1

3

Building on Qumara's comment, you should call point.x.to_i and point.y.to_i.

You should also remove the quotes around your array parameter.

The line in question inside your JBuilder block should look like

json.point [point.x.to_i,point.y.to_i]
Sign up to request clarification or add additional context in comments.

1 Comment

It works! I have added a new method in the Point model to returns an array of the points, that is the same to your answer. Thanks!!

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.