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

折月煮酒 提交于 2019-12-03 18:00:49

问题


I have a troubles with integrating Rails routing with AngularJS.

When I go to "localhost:3000" in browser input, it redirect me to the "localhost:3000/#/auth/sign_in"

but 'sign_in' template not render.

Then, secondary if I go to the "localhost:3000/#/", it redirect me to the "localhost:3000/#/auth/sign_in" and render 'sign_in' template right.

How to fix it, when I go to "localhost:3000" then render 'sign_in' ?

Router code here: http://pastie.org/8917505

Thanks!


回答1:


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'


来源:https://stackoverflow.com/questions/22394014/how-to-correct-set-root-route-in-rails-4-for-angularjs

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