# האם ניתן לסריאל את הבקר?

**URL:** <https://meta.discourse.org/t/can-you-serialize-a-controller/262156>\
**Category:** Development\
**Created:** [19 באפריל,‏ 2023,‏ 12:21am UTC](https://meta.discourse.org/t/can-you-serialize-a-controller/262156 "2023-04-19T00:21:22Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![mqmenchaca](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@mqmenchaca](https://meta.discourse.org/u/mqmenchaca)\
**Post date:** [19 באפריל,‏ 2023,‏ 12:21am UTC](https://meta.discourse.org/t/can-you-serialize-a-controller/262156/1 "2023-04-19T00:21:22Z")

</div>

Hi Discourse community. I didn’t find a similar topic so I thought I’d start one.

I’m using a plugin to extend Discourse functionality for my organization’s purposes. One of these is rendering a JSON from `email/unsubscribe/{KEY}`, as we’d like to control these options from a different server than the one we run our Discourse instance. That endpoint is under the purview of the EmailController, and by default the EmailControllerHelper sets instance variables for the controller before rendering a view. Instead, I’d like to return the EmailController as a JSON.

So my question at base is, is it possible to serialize a controller? i.e., can I define a serializer for the controller like this:

```ruby
  class ::EmailController
    # THE FILE THIS MODIFIES: app/controllers/email_controller.rb
    # Return a json

    module ReturnAsJSON
      def unsubscribe
        super

        render EmailUnsubscribeSerializer.new(self, root: false).to_json
      end
    end

    prepend ReturnAsJSON
  end

  class EmailUnsubscribeSerializer < ApplicationSerializer
    attributes :topic, :type, :unsubscribed_from_all, :user

    def user
      BasicUserSerializer.new(object.user, root: false).as_json
    end
  end

```

or am I better off brute forcing this?

```ruby
  class ::EmailController
    # THE FILE THIS MODIFIES: app/controllers/email_controller.rb
    # Return a json

    module ReturnAsJSON
      def unsubscribe
        super

        render json: {type: @type, unsubscribed_from_all: @unsubscribed_from_all, etc.}
      end
    end

    prepend ReturnAsJSON
  end

```

The 2nd approach looks like it works, so if the 1st approach doesn’t at least I have a decent workaround.

---

_[View the full topic](https://meta.discourse.org/t/can-you-serialize-a-controller/262156)._
