【发布时间】:2019-02-28 00:24:10
【问题描述】:
我必须对用 Ruby On Rails 开发的 API 进行更改,如下所示:
class V0::PythonsController < ApplicationController
skip_before_action :authorize_request
# POST v0/python/import
def import
result = { status: :error }
data = eval(AwsTool.decrypt(params["code"])).first
if data.class == Hash
user = User.find_by(id: data[:ot_user_id])
activity_type = ActivityType.find_by(id: data[:activity_type])
if user.nil?
result[:msg] = "user not found"
elsif activity_type.nil?
result[:msg] = "activity type not found"
else...
我将“代码”参数中的一些数据传递给它,然后对其进行解密和探索。我想添加一个 if 子句,所以当我从不同的来源调用 API 时,不会发生加密和解密。所以我做了这个改变:
class V0::PythonsController < ApplicationController
skip_before_action :authorize_request
# POST v0/python/import
def import
result = { status: :error }
if params["origin"] != 'trusted'
data = eval(AwsTool.decrypt(params["code"])).first
else
data = params["code"]
end
if data.class == Hash
user = User.find_by(id: data[:ot_user_id])
activity_type = ActivityType.find_by(id: data[:activity_type])
...
问题在于 data.class 不是一个 Hash 对象,它是一个字符串。我尝试了不同的解决方案来将对象从 String 转换为 Hash,例如 t_hash 和其他类似函数,但它们不起作用。我收到了一些关于参数不被允许的错误,我尝试将许可证添加到它们但仍然失败。
还有什么想法吗?
【问题讨论】:
-
data.class 是什么样的?
-
如果我尝试
p data.class,我会得到字符串 -
你的
params["code"]是什么样的? -
如果你只尝试
p data会怎样? -
大家好,如果我输入
p data,结果是这样的:{:ot_user_id=>"3", :query_date_time=>"2019-02-27T22:00:21:848587", :theactivities=>[{:activity_value=>"5500", :activity_date_time=>"2019-02-27"}]}
标签: ruby-on-rails api types parameters applicationcontroller