Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
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 for details. –  Sunil D. Apr 9 at 4:56

1 Answer 1

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.

share|improve this answer
    
Can you provide an example using the code I have provided? Im not fully understanding –  dgolman Apr 9 at 2: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. stackoverflow.com/questions/16677528/… –  Papzord Apr 9 at 16:01
    
when I do that it just keeps redirecting me to that page regardless of the url I enter. –  dgolman Apr 9 at 18:28
    
That's true. But now the angular routes should do their job. –  Papzord Apr 9 at 20:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.