2

I'm using Rails 4.4.1, Ruby 2.1.2, RGeo 0.3.20 and activerecord-mysql2spatial-adapter 0.4.3

My problem is probably very simple since I'm new to both Ruby and Rails, but I could not find anything useful in the web so far.

I want to create a form to insert geo-spatial coordinates in my db, but I don't know how to access the :latlon fields x and y. This is my tentative code:

<h1>Inserimento nuova Città</h1>
<%= form_for @city, url: cities_path do |city| %>
    <p>
        <%= city.label :name, "Nome"%><br>
        <%= city.text_field :name %>
    </p>

    <p>
        <%= city.label :latlon, "Coordinate GPS" %><br>
        <%= city.number_field :latlon.x %><br>
        <%= city.number_field :latlon.y %><br>
    </p>
<% end %>

The error I get when I access the localhost:3000/cities/new url is

undefined method `x' for :latlon:Symbol

Anyone knows how do I create a form to insert the latlon.x and latlon.y data in my db?

sparviero19
  • 63
  • 2
  • 5
  • What is the value for `city.latlon`? – Max Williams Jun 06 '14 at 13:27
  • the datatype is point (http://daniel-azuma.com/articles/georails/part-3), the values should be two numeric values, es (43.11, 12.38) – sparviero19 Jun 06 '14 at 13:52
  • I've found a workaround for now. I edited the cities_controller.rb adding the factory method: `self.rgeo_factory_generator = RGeo::Geos.method(:factory)` Then I have modified the form_for to handle text input: `

    <%= city.label :latlon, "Coordinate GPS" %>
    <%= city.text_field :latlon %>

    ` But now to insert a correct value the user has to write POINT(43.11 12.38). I'm still looking for a solution to input the two numbers separatley, without any text 'POINT'
    – sparviero19 Jun 06 '14 at 14:07

1 Answers1

2

You can't call city.number_field :latlon.y because here :latlon is just a symbol - it's telling the helper to call the "latlon" method, and to set the name to "city[latlon]".

One way to get around this is to add get and set methods for the individual x/y values. These may exist already, added by the gem, i don't know as i've not used it. But you could add

class City < ActiveRecord::Base
  def latlon_x
    self.latlon.x
  end

  def latlon_y
    self.latlon.y
  end

  def latlon_x=(num)
    self.latlon.x = num
  end

  def latlon_y=(num)
    self.latlon.y = num
  end

Now you could say, in your form,

    <%= city.number_field :latlon_x %><br>
    <%= city.number_field :latlon_y %><br>

this will use latlon_x to get the value, and will cause latlon_x= to be called when you do @city.update_attributes(params[:city]) in your update action.

Max Williams
  • 32,435
  • 31
  • 130
  • 197