How to correct set root route in Rails 4 for AngularJS?

ぐ巨炮叔叔 提交于 2019-11-29 04:59:15
Sasha B.

I have found the solution.

You need to enable HTML5 mode for AngularJS:

angular.module('app').config(['$routeProvider', '$locationProvider',
  function ($routeProvider, $locationProvider) { 
  ...
    $locationProvider.html5Mode(true);
  }]
)

You also need to add the <base> html tag:

<html>
  <head>
    <base href="/">
      ...
  </head>
</html>

It fixes Angular's incorrect processing of URL-path when you press Enter in the browser URL input again. Without this, the last 'domain' will be repeated:

localhost:3000/auth/sign_in

localhost:3000/auth/auth/sign_in

...

You also need to write correct route on server side (in Rails) for all SPA routes:

config/routes.rb

  root 'application#main'

  get '*path', to: 'application#main'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!