在Ruby on Rails中格式化/解析表单值 [英] formatting/parsing form values in Ruby on Rails

查看:81
本文介绍了在Ruby on Rails中格式化/解析表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RGeo(PostGIS)和Ruby on Rails,两者都是新手.我有一个包含坐标属性(纬度和经度)的模型.绑定具有该属性的表单时,我正在使用文本输入字段,但是我想将值格式化为人类可读的表单.目前,它正被渲染"为原始DB值,即"0101000020E61000009468C9E3698C5EC065FED13769D64740".我想将其格式设置为用户的"-122.193963,47.675086".相反,如果用户更新该值,则需要将其解析为PostGIS可以理解的格式.

I am working with RGeo (PostGIS) and Ruby on Rails, total newbie to both. I've got a model that contains a coordinates attribute (latitude & longitude). I am using a text input field when binding a form that that attribute, but I wanted to format the value into a human readable form. Currently it is being 'rendered' as the raw DB value, i.e., '0101000020E61000009468C9E3698C5EC065FED13769D64740'. I'd like to format that as something like '-122.193963, 47.675086' for the user. Conversely, if the user updates the value, I will need to parse it into a format that PostGIS can understand.

我一直在搜索和阅读有关在RoR中将数据绑定到表单的信息,但是找不到任何有用的信息.

I've been searching and reading what I can find about binding data to forms in RoR, but can't find anything useful.

推荐答案

您看到的是众所周知的二进制文件(WKB).有许多几何访问器可获取不同格式或表示形式的坐标信息.例如:

You are seeing a hex-encoded representation of well-known binary (WKB). There are many geometry accessors to get coordinate information in different formats or representations. For example:

SELECT ST_AsText(geom) AS WKT, ST_Y(geom) AS latitude, ST_X(geom) AS longitude
FROM (
  SELECT '0101000020E61000009468C9E3698C5EC065FED13769D64740'::geometry AS geom
) AS f;

             wkt              | latitude  |  longitude
------------------------------+-----------+-------------
 POINT(-122.193963 47.675086) | 47.675086 | -122.193963

在Ruby RGeo方面,可以使用 WKBParser .例如,在 tc_wkb_parser.rb 中,您可以做一些事情相似:

On the Ruby RGeo side, similar information can be obtained using WKBParser. For example, in tc_wkb_parser.rb you could do something similar:

parser_ = ::RGeo::WKRep::WKBParser.new
obj_ = parser_.parse('0101000020E61000009468C9E3698C5EC065FED13769D64740')
print [obj_.y, obj_.x]  # [-122.193963, 47.675086]

这篇关于在Ruby on Rails中格式化/解析表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆