gpt4 book ai didi

javascript - Ruby on Rails 生成 Json

转载 作者:行者123 更新时间:2023-12-03 01:47:57 25 4
gpt4 key购买 nike

我有一个生成 Json 的 Controller 。我使用该 Json 在 Google map 中显示记录,但我只想显示那些具有参数 toll=="true" 的记录,没有的不会显示。

我正在考虑直接在 Json 中执行此过滤器(通过生成的 url),当我通过 javascript 调用此过滤器时,我使用我需要的参数调用 url。如何仅通过 url 访问具有参数 toll=="true" 的 URL ?示例:localhost:3000/map_pis?tool=="true" ?:

class PointOfInterestsController < ApplicationController
def map
pis = PointOfInterest.all
fares = FareCity.all
msg = '
{
"pis": [
'
total_pis = ''
pis.each do |p|
total_pis += '
{"kilometer": ' + p.kilometer.to_s + ',
"category": "pis",
"name": "' + p.name + '",
"show_on_map": "' + p.point_of_interest_category.show_on_map.to_s + '",
"lat": ' + p.lat.to_s + ',
"lng": ' + p.lng.to_s + ',
"icon": "' + p.point_of_interest_category.icon.url + '",
"description": "' + p.description + '",
"image": "' + p.image.url + '"},'
end

fares.each do |f|
total_pis += '
{"kilometer": ' + f.kilometer.to_s + ',
"category": "fare",
"name": "' + f.name + '",
"toll": "' + f.is_toll.to_s + '",
"lat": ' + f.lat.to_s + ',
"lng": ' + f.lng.to_s + ',
"icon": "/img/icon_fare_map.png",
"pedagios": ['
total_fares = ''

f.fare_pricings.each do |fp|
total_fares += '
{"title": "' + fp.fare_category.title.to_s + '",
"price": ' + fp.price.to_s + '},'
end

total_pis += total_fares[0...-1]

total_pis += '] },'
end

msg += total_pis[0...-1]

msg +='
]
}
'
render :json => msg
end
end

谢谢。

最佳答案

假设您所说的 toll=="true" 指的是 FareCity 模型的属性 is_toll(true/false),您可以直接在 Controller 内部进行条件显示:

def map
pis = PointOfInterest.all
fares = FareCity.where(is_toll: true)
#...
end

正如上面所说,只有 is_toll 为 true 的票价才会在 json 中呈现。

关于javascript - Ruby on Rails 生成 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50547554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com