我正在努力尋找能夠幫助您的專家。我正在編寫一個Ruby 2.0.0p247(Rails 3.2.13)的API與一組控制器,可以調用一個名爲send_response,它採用狀態和可選參數,並格式化爲一個適當的HTTP響應然後通過JSON將其返回給最終用戶,然後停止所有執行。 API調用受數據庫支持的令牌的保護。Ruby在函數中使用'return false'時不會停止執行
我正在使用render json: response, :status => response['status'] and return false'
但執行似乎繼續,無論如何。例如:API調用添加一個日誌條目,我使用了一個錯誤的參數讓我得到一個JSON響應,說明錯誤的參數,但日誌條目仍然被添加。
的來源「send_response()調用:
# Make sure the user is using the correct parameters, otherwise kick 'em out
params.each do |key,value|
if params_list.include?(key) == false
send_response 500, data: 'Invalid parameters used!'
end
end
我完全茫然,爲什麼執行:
def send_response(status, options = {})
source = options[:source] || nil
count = options[:count] || -1
data = options[:data] || nil
id = options[:id] || -1
trigger_id = options[:trigger_id] || -1
response = {}
case status
when 'ok', 200
response['status'] = 200
response['message'] = 'success'
response['count'] = count unless count == -1
response['data'] = data unless data == nil
response['id'] = id unless id == -1
response['trigger_id'] = trigger_id unless trigger_id == -1
when 'bad_request', 400
response['status'] = 400
response['message'] = 'error'
response['data'] = data unless data == nil
when 'unauthorized', 401
response['status'] = 401
response['message'] = 'unauthorized'
response['data'] = data unless data == nil
when 'internal_error', 500
response['status'] = 500
response['message'] = 'error'
response['data'] = data unless data == nil
else
response['status'] = 500
response['message'] = 'error'
response['data'] = 'Invalid send_response() sent.'
end
response['source'] = caller[0][/`.*'/][1..-2] if Rails.env.development?
render json: response, :status => response['status'] and return false
end
這個功能的一個例子我的控制器被稱爲沒有結束,任何幫助將不勝感激指向正確的方向。
在此先感謝!當前方法的
--Jayson
非常感謝你 - 它完美的作品! –