How to Remove the Hash From Angular Routes in Rails

When building client-side applications, your URLs are prepended with a hash. Removing this can make your routes more user-friendly and more easily crawled by search engines. Here is how you can remove the hash from your routes in an Angular app built on Ruby on Rails, and use the standard URL structure.

Step 0: Our example Application

We’ll look at a recipe site for our example application. In our application, we’ll have a homepage, a recipe route that shows us all of the recipes, and a route for single recipes. Here is our application configuration:

<br />
var app = angular.module(&quot;recipeapp&quot;);</p>
<p> app.config([&quot;$routeProvider&quot;,function($routeProvider) {<br />
   $routeProvider<br />
     .when(&quot;/&quot;, {<br />
       templateUrl: &quot;templates/index.html&quot;,<br />
       controller: &quot;IndexController&quot;<br />
     })<br />
     .when(&quot;/recipes/, {<br />
       templateUrl: &quot;templates/recipes.html&quot;,<br />
       controller: &quot;RecipesController&quot;<br />
     })<br />
     .when(&quot;/recipes/:id&quot;, {<br />
       templateUrl: &quot;templates/recipe_show.html&quot;,<br />
       controller: &quot;RecipeShowController&quot;<br />
     });<br />
 }]);<br />

Step 1: Remove The Hash

First, we will enable [HTML5 Mode] in our AngularJS application. This will remove the hash from our URLs. Include $locationProvider in your module’s configuration, and set the mode to true.

<br />
app.config(['$routeProvider','$locationProvider', function($routeProvider, $locationProvider) {<br />
   $locationProvider.html5mode(true);<br />
   // ...<br />
  }]);<br />


Step 2: Configure Rails To Handle Angular Routes

The above will work for cases when the user navigates from one part of the AngularJS application to another. However, if they try to navigate directly, they will get an error. If the user tries to navigate to /recipes/1 directly or hits refresh on that page, the request is sent to the Ruby on Rails router instead of Angular.

To fix this, we need to capture those requests. In you app/config/routes.rb at the end:

<br />
get '/\*path' =&gt; redirect('/?goto=%{path}')<br />

This will cause any uncaught routes to get redirected as a query string, which will be passed to the Angular router.

Step 3: Handle Rails Redirects

Angular needs to know how to handle these routes, so we will add a call to the otherwise() method of $routeProvider:

<br />
 $routeProvider.otherwise({<br />
   redirectTo: function(current, path, search) {<br />
     if(search.goto) {<br />
       return '/' + search.goto;<br />
     }<br />
       return '/'<br />
   }<br />
 });<br />

Now any URL will get redirected to Angular, which will handle the route. This way you don’t have to maintain two lists of routes, and you can use standard URL formats in your application.

Miss your last deadline? Download a Free Chapter of “Dependable: Deliver Software on Time and within Budget”
Click Here to Leave a Comment Below 2 comments
Nirav Gandhi - a couple of years ago

This is great. I found a simpler way to handle this. No need to do step 3
get ‘*path’, :to => redirect(‘/#/%{path}’)

    Steve - 11 months ago

    Thanks for this answer! Worked for me except my version of angular needed get ‘*path’, :to => redirect(‘/#!/%{path}’) the apostrophe


Leave a Reply: