File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -314,6 +314,38 @@ So that when we render the `#edit` action, we'll get
314314
315315Where ` can_edit ` is ` view_context.current_user.admin? ` (true).
316316
317+ You can also tell what to set as ` serialization_scope ` for specific actions.
318+
319+ For example, use ` admin_user ` only for ` Admin::PostSerializer ` and ` current_user ` for rest.
320+
321+ ``` ruby
322+ class PostsController < ActionController ::Base
323+
324+ before_action only: :edit do
325+ self .class .serialization_scope :admin_user
326+ end
327+
328+ def show
329+ render json: @post , serializer: PostSerializer
330+ end
331+
332+ def edit
333+ @post .save
334+ render json: @post , serializer: Admin ::PostSerializer
335+ end
336+
337+ private
338+
339+ def admin_user
340+ User .new (id: 2 , name: ' Bob' , admin: true )
341+ end
342+
343+ def current_user
344+ User .new (id: 2 , name: ' Bob' , admin: false )
345+ end
346+ end
347+ ```
348+
317349#### #read_attribute_for_serialization(key)
318350
319351The serialized value for a given key. e.g. ` read_attribute_for_serialization(:title) #=> 'Hello World' `
You can’t perform that action at this time.
0 commit comments