从两个哈希构建数组
Build array from two hashes
我尝试使用地理json 数据构建json。
在我的控制器中:
def index
....
respond_to do |format|
format.html
format.json { render json: { type: 'FeatureCollection', features: pois_geojson + tracks_geojson} }
end
和表演
def show
...
respond_to do |format|
format.html
format.json { render json: { type: 'FeatureCollection', features: poi_geojson + track_geojson} }
end
对于索引,一切正常,我的 json 很好。我将此方法称为构建 json.
显示方法
def poi_geojson
{
type: 'Feature',
RGeo::GeoJSON.encode(@poi.lonlat),
properties: {
name: @poi.name,
:'marker-color' => '#00607d',
:'marker-symbol' => 'circle',
:'marker-size' => 'medium'
}
}
end
def track_geojson
{
type: 'Feature',
geometry: RGeo::GeoJSON.encode(@track.path),
properties: {
:'color' => '#ff7800',
:'weight' => '5',
:'opacity' => '0.65'
}
}
end
索引方法
def pois_geojson
@pois.map do |poi|
{
type: 'Feature',
RGeo::GeoJSON.encode(poi.lonlat)
properties: {
name: poi.name,
:'marker-color' => '#00607d',
:'marker-symbol' => 'circle',
:'marker-size' => 'medium'
}
}
end
end
def tracks_geojson
@tracks.map do |track|
{
type: 'Feature',
geometry: RGeo::GeoJSON.encode(track.path),
properties: {
:'color' => '#ff7800',
:'weight' => '5',
:'opacity' => '0.65'
}
}
end
end
如您所见,方法相似,但我不明白为什么索引可以正常工作,而显示却不行。
我有这个错误:
`undefined method '+' for #`
for this line :
`format.json { render json: { type: 'FeatureCollection', features: poi_geojson + track_geojson} }`
散列实例没有+
方法,要从两个散列中形成一个数组,您可以执行以下操作:
[pois_geojson, tracks_geojson]
这对 pois_geojson
和 tracks_geojson
起作用的原因是因为它们都已经是数组。
我尝试使用地理json 数据构建json。
在我的控制器中:
def index
....
respond_to do |format|
format.html
format.json { render json: { type: 'FeatureCollection', features: pois_geojson + tracks_geojson} }
end
和表演
def show
...
respond_to do |format|
format.html
format.json { render json: { type: 'FeatureCollection', features: poi_geojson + track_geojson} }
end
对于索引,一切正常,我的 json 很好。我将此方法称为构建 json.
显示方法
def poi_geojson
{
type: 'Feature',
RGeo::GeoJSON.encode(@poi.lonlat),
properties: {
name: @poi.name,
:'marker-color' => '#00607d',
:'marker-symbol' => 'circle',
:'marker-size' => 'medium'
}
}
end
def track_geojson
{
type: 'Feature',
geometry: RGeo::GeoJSON.encode(@track.path),
properties: {
:'color' => '#ff7800',
:'weight' => '5',
:'opacity' => '0.65'
}
}
end
索引方法
def pois_geojson
@pois.map do |poi|
{
type: 'Feature',
RGeo::GeoJSON.encode(poi.lonlat)
properties: {
name: poi.name,
:'marker-color' => '#00607d',
:'marker-symbol' => 'circle',
:'marker-size' => 'medium'
}
}
end
end
def tracks_geojson
@tracks.map do |track|
{
type: 'Feature',
geometry: RGeo::GeoJSON.encode(track.path),
properties: {
:'color' => '#ff7800',
:'weight' => '5',
:'opacity' => '0.65'
}
}
end
end
如您所见,方法相似,但我不明白为什么索引可以正常工作,而显示却不行。
我有这个错误:
`undefined method '+' for #` for this line : `format.json { render json: { type: 'FeatureCollection', features: poi_geojson + track_geojson} }`
散列实例没有+
方法,要从两个散列中形成一个数组,您可以执行以下操作:
[pois_geojson, tracks_geojson]
这对 pois_geojson
和 tracks_geojson
起作用的原因是因为它们都已经是数组。