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?

1
  • 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. Commented Apr 9, 2014 at 4:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you provide an example using the code I have provided? Im not fully understanding
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/…
when I do that it just keeps redirecting me to that page regardless of the url I enter.
That's true. But now the angular routes should do their job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.