title: Conditional redirect in ember before/upon page load
body: <p>I'm trying to write some logic where if a user is logged in (and hence a <code>currentUser</code> variable is defined, more details in edit section below), he should not be able to access the root URL of the application, which is the log-in page.  I'm trying to achieve this using a redirect to the <code>users.show</code> page before/as soon as the page loads, but I can't seem to figure out how to do it.</p>
  
  <p>Reading ember documentation, you can do redirects from the route or the controller.  I've attempted both and neither have worked.  In the route:</p>
  
  <pre><code>App.ApplicationRoute = Ember.Route.extend
    redirect: -&gt;
      currentUser = @controllerFor('Application').get('currentUser') # this works
      currentPath = @controllerFor('Application').get('currentPath') # this returns undefined
      if currentUser &amp;&amp; currentPath == '/'
        @transitionTo('users.show', currentUser)
  </code></pre>
  
  <p>The problem here is that I can't read the currentPath for some reason, even though I can access it from within <code>App.ApplicationController</code>.</p>
  
  <p>In the controller, I've tried:</p>
  
  <pre><code>App.ApplicationController = Ember.Controller.extend
    init: -&gt; # never gets called
      if @get('currentUser') &amp;&amp; @get('currentPath') == '/'
        @transitionToRoute('users.show', @get('currentUser'))
  </code></pre>
  
  <p>Except the <code>init</code> never gets called when loading the page, and I can't figure out from the documentation which hook gets called on page load.</p>
  
  <p>If there are any other problems with the code, feel free to point them out.  How do I fix my code in order to achieve the redirect?</p>
  
  <p><strong>EDIT:</strong> it was suggested for the route implementation to change <code>'application'</code> to lowercase in <code>@controllerFor('application')</code>.  Tried this and it returns the error </p>
  
  <pre><code>Assertion failed: The controller named 'application' could not be found. Make sure that this route exists and has already been entered at least once. If you are accessing a controller not associated with a route, make sure the controller class is explicitly defined.
  </code></pre>
  
  <p>Not sure what's going on, as doing a <code>@controllerFor</code> for other routes in all lowercase works, but not <code>'application'</code>.</p>
  
  <p>The <strong>Current User</strong> variable is obtained via an injection in an ember initializer.  I'm using Rails and Devise on my backend, and the way I get <code>currentUser</code> into ember essentially more or less follows <a href="http://say26.com/using-rails-devise-with-ember-js" rel="nofollow">this tutorial</a>.  The general flow is Rails -> meta tag -> initializer -> injection.</p>
  
  <p><strong>SOLUTION:</strong> Inspired by the discussion following the accepted answer, I ended up doing it in <code>ApplicationRoute</code> like so:</p>
  
  <pre><code>App.ApplicationRoute = Ember.Route.extend
    beforeModel: (transition) -&gt;
      currentUser = @controllerFor('currentUser')
      if currentUser &amp;&amp; transition.targetName == 'index'
          @transitionTo('users.show', currentUser)
  </code></pre>