3

I have the following code in my angular app.

app.config(function($routeProvider, $locationProvider) {
   $locationProvider.html5Mode(true);
   $routeProvider
     .when("/users/:u_id/restaurants/:r_id/menus/:m_id/sections/:sec_id/items", { templateUrl: "/assets/menus/items.html", controller: "MenuItemCtrl" })
});

It works fine when I navigate to the path using

<a href="./sections/{{section.id}}/items">View Items</a>

But when I refresh the page or go directly to the url it throws a rails routing error

No route matches [GET] "/users/3/restaurants/3/menus/4/sections/4/items"

If I create this route in routes.rb it just returns the JSON associated with the response and does not route to the correct template.

Does anyone know how to fix this issue?

dgolman
  • 196
  • 1
  • 13
  • Add a "catch all" route at the end of your routing config in Rails: `get '(*url)' => 'home#index'` In this example the home_controller's index() method serves up the single page app. See [this answer](http://stackoverflow.com/a/12100208/398606) for details. – Sunil D. Apr 09 '14 at 04:56

1 Answers1

5

In the rails route you must redirect that request to the angular base url, so angular can be executed.

Without that, the rails responds to the request. That's how html5mode routes work.

Edit:

Let's say that your angular is served from application#home. You want all the GET requests to be redirect to there, so angular can work.

Adding this route get '*path', to: 'application#home' will make all the GET requests the angular page.

Papzord
  • 408
  • 4
  • 14
  • Can you provide an example using the code I have provided? Im not fully understanding – dgolman Apr 09 '14 at 02:10
  • This might bring some issues when fetching JSON, but you can add `format: :html` to the route. Also, using hashbang mode instead of html5 is simpler. This answer may fit you. http://stackoverflow.com/questions/16677528/location-switching-between-html5-and-hashbang-mode-link-rewriting – Papzord Apr 09 '14 at 16:01
  • when I do that it just keeps redirecting me to that page regardless of the url I enter. – dgolman Apr 09 '14 at 18:28
  • That's true. But now the angular routes should do their job. – Papzord Apr 09 '14 at 20:00