我只能在 url 中传递一个参数,由于我不能传递多个参数,我有如下硬编码参数。如何在 Rails 中传递和获取多个参数?
#This value getting from url
address = params[:id]
#This value hard coded
parameter = "SSID"
我能够从以下请求中得到响应
http://localhost:3000/apitest/v1/get_request/10:11:55
但我需要传递如下请求
http://localhost:3000/apitest/v1/get_request/10:11:55,SSID
在我正在使用的代码下方。
module ApiTest
module V1
class GetRequestController < ApplicationController
def show
value = ''
address = params[:id]
parameter = "SSID"
client = ClientApi.new
response = client.get_call(address, parameter)
redis_client = Redis.new
access_token = redis_client["client:SATToken"]
if access_token.nil?
sat_client = SatClient.new
response = sat_client.get_sat_token
body = JSON.parse(response.body)
if response.code == "200"
access_token = body["access_token"]
redis_client = Redis.new
redis_client["client:SATToken"] = access_token
return access_token
else
p "SAT endpoint returned #{response.code}"
p "#{response.body}"
raise "Retrieving SAT Token call failed"
end
end
puts "response.status_code #{response.code}"
if response.code == "200"
value = JSON.parse(response.body)["parameters"][0]["value"]
render json: value
end
end
end
end
end
路线
namespace 'apitest' do
namespace 'v1' do
resources :get_request_controller
end
end
如果您命名它们,则可以传递多个参数
http://localhost:3000/apitest/v1/get_request/10:11:55?parameter=SSID
这保存在params[:parameter]
. 然后您可以通过在 & 符号后添加它们来添加更多内容:
http://localhost:3000/apitest/v1/get_request/10:11:55?parameter=SSID¶meter2=something
你在哪里可以得到 params[:parameter2]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句