2

I want to have ability to edit GeoJSON data as text, in edit page. I use Rails, PostgreSQL with activerecord-postgis-adapter. For encoding data I use rgeo-geojson.

My show view works fine, I encode:

<%= RGeo::GeoJSON.encode(@field.shape, json_parser: :json) %>

But how to upgrade my edit view, so I could edit data in GeoJSON format and save it:

<%= form_for :field, url: field_path(@field), method: :patch do |f| %>
...
  <p>
    <%= f.label :shape %><br>
    <%= f.text_area :shape %>
  </p>
...

<% end %>

Sorry if the question look messy

4

1 回答 1

2

您可以添加一个虚拟属性来Field建模一个虚拟属性,它将 postgis db 列转换为 GeoJSON 并返回:

class Field < ActiveRecord::Base
  def shape_text
    RGeo::GeoJSON.encode(shape).to_json
  end

  def shape_text=(text)
    self.shape = RGeo::GeoJSON.decode(text, json_parser: :json)
  end
end


<%= form_for :field, url: field_path(@field), method: :patch do |f| %>
...
  <p>
    <%= f.label :shape_text %><br>
    <%= f.text_area :shape_text %>
  </p>
...

<% end %>
于 2017-05-05T15:41:40.303 回答