# Allow users request access to private categories: rendering custom template on exception

**URL:** https://meta.discourse.org/t/allow-users-request-access-to-private-categories-rendering-custom-template-on-exception/45083
**Category:** Development
**Created:** [June 1, 2016, 12:41pm UTC](https://meta.discourse.org/t/allow-users-request-access-to-private-categories-rendering-custom-template-on-exception/45083 "2016-06-01T12:41:18Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![rilla](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rilla/32/121738_2.png) [@rilla](https://meta.discourse.org/u/rilla)
#### Post date: [June 1, 2016, 12:41pm UTC](https://meta.discourse.org/t/allow-users-request-access-to-private-categories-rendering-custom-template-on-exception/45083/1 "2016-06-01T12:41:18Z")

</div>

Hi all,

I’m working on [this plugin](https://github.com/rilla/discourse-show-private-categories) which adds a setting to allow showing read-restricted categories in the category list.

This is already working (you can check the code in the Github repo), but now, as a next step, I want to show a custom view when a user with no access clicks on a restricted category.

My intention is to add a message to that view explaining that the category is private and encourage the user to register and request access to the category.

The way I’m doing this is adding a new `NoAccessToCategory` exception that is thrown whenever a user tries to access the restricted category, and now I’m trying to rescue from this category and show a custom template.

This is what I’ve done so far:

1. Declare the new exception:

```ruby
 # lib/discourse.rb
class NoAccessToCategory < StandardError; end

```

1. Tweak `CategoryGuardian` so it throws `NoAccessToCategory` instead of `NotFound`:

```plaintext
# lib/guardian/category_guardian.rb
def ensure_can_see!(obj)
   raise Discourse::NoAccessToCategory.new("Can't see #{obj}") unless can_see?(obj)
end

```

1. Rescue in `ApplicationController`:

```ruby
  # app/controllers/application_controller.rb
  rescue_from Discourse::NoAccessToCategory do
    if (request.format && request.format.json?) || (request.xhr?)
      # No idea what to do here
    else
      render_to_string status: 200, layout: 'no_ember', formats: [:html], template: '/exceptions/no_access_to_category'
    end
end

```

1. Build the view:

```erb
  # app/views/exceptions/no_access_to_category.html.erb
  <h1>No access to this category</h1>
  <div>
     You don't have access to this category
  </div>

```

This is working when I browse directly to a restricted category url (i.e. no AJAX request), but it’s not working when the category is accessed by clicking through the Ember frontend.

I’m completely unfamiliar with Ember (I come from a Rails background), and I would be very grateful if someone could give me some pointers as to how should I implement this.

Once it’s done, I will of course release this and hopefully will be of some use for other users – I’ve seen this requested in a couple of threads: [here](https://meta.discourse.org/t/feature-allowing-a-private-category-to-be-seen-but-topics-remain-hidden/21429) and [here](https://meta.discourse.org/t/asking-for-access-to-a-private-category/21135).

---

<div class="post-metadata">

### Author: ![jord8on](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jord8on/32/166809_2.png) [@jord8on](https://meta.discourse.org/u/jord8on)
#### Post date: [February 9, 2019, 4:00am UTC](https://meta.discourse.org/t/allow-users-request-access-to-private-categories-rendering-custom-template-on-exception/45083/2 "2019-02-09T04:00:38Z")

</div>

Hello @rilla! Were you able to get this working?
