[英]Ruby/Rails: Why is render json: {hello: 'world'} hitting my database?
我有一個動作:
def test
_process_action_callbacks.map { |c| pp c.filter }
render json: {hello: 'world'}
end
由於某種原因,它正在調用在我的應用程序控制器中定義的current_user函數。
起初,我認為這是一個調用我的current_user函數(因此稱為_process_action_callbacks)的操作。 但是在剝離了我所有的動作之前,電話仍然存在。 之前僅有的兩個動作是rails的一部分:
:clean_temp_files
:set_turbolinks_location_header_from_session
我使用調用程序來查看從何處調用我的方法。 這是stacktrace(和方法聲明):
def current_user
pp caller
# get the current user from the db.
end
如您所見,current_user函數由序列化類中的serialization_scope方法調用。 如何防止它調用我的current_user函數?
您的標簽表明您正在使用active-model-serializers
。 默認情況下, current_user
是作用域。 要自定義應用程序控制器中定義的范圍,您可以執行以下操作
class ApplicationController < ActionController::Base
serialization_scope :current_admin
end
上面的示例將范圍從current_user
(默認)更改為current_admin
。
在您的情況下,您可能只想在控制器中設置范圍(我假設它稱為SomeController
;)),您可以編寫
class SomeController < ApplicationController
serialization_scope nil
def test
render json: {hello: 'world'}
end
end
請參閱以獲取完整的文檔: https : //github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.