Search code examples
javascripthttp-redirectember.jsember-router

How to transition to 404 "catch-all" style route?


I have a subdomain look up inside my index route. It looks up a plan for the subdomain slug and redirects to the plan page. If no plan returns it needs to render the 404 page. Here is the code for the redirect. Admittedly I'm new to ember, but this code seems to be correct. I get zero errors in the console and it simply goes to the route I'm trying to redirect away from:

controller/index.js

model: function() {
    var self = this;

    if ( this.isHomePage() ) {

      return Ember.Object.create();

    } else {

      // Get resources
      var parts = window.location.hostname.split('.');
      var subdomain = parts[0];

      return this.store.find('plan', { plan_subdomain: subdomain }).then(function(plans) {
        var returnedPlan = plans.get('firstObject');
        if (Ember.isEmpty(returnedPlan)){
          debugger;
          return self.transitionTo('fourOhFour');
        } else {
          return returnedPlan;
        }
      });

    }
  },

router.js

  this.route("fourOhFour", { path: "*path"});

Solution

  • You are specifying your 404 path as a catch all

    this.route("fourOhFour", { path: "*path"});
    

    which makes sense, but then you are telling Ember to transition into that wildcard route, but what would the URL be? /*?

    You need to tell Ember what that path should be by specifying a second parameter to transitionTo()

    See here for an example

    I answered a similar question here