简体   繁体   English

Ruby on Rails和UI Router无法正常工作

[英]Ruby on rails and UI Router not working properly

Alright... So I've installed UI Router and Rails-templates to handle AngujarJs in my project. 好的...所以我已经安装了UI Router和Rails模板来处理我项目中的AngujarJs。 So far everthing is fine. 到目前为止,一切都很好。 Except if a type the url manually into chrome's bar (eg localhost:3000/sign_in) I get 'No routes matches for /sign_in' (Ruby error). 除非在chrome的栏中手动输入url(例如localhost:3000 / sign_in),否则会得到“ / sign_in没有匹配的路由”(Ruby错误)。

But if in my app I have a button for changing my state ($state.go) to that sign_in view, works just fine. 但是,如果在我的应用程序中有一个用于将状态($ state.go)更改为该sign_in视图的按钮,则可以正常工作。

What's happening? 发生了什么?

What's happening there is that your angular app is intercepting the changes to the location bar for it's own purposes, and preventing the browser from making a http request to the new url, so all is well after the app has initialized. 发生的情况是您的角度应用程序出于其自身目的拦截了位置栏的更改,并阻止了浏览器向新网址发出http请求,因此在应用程序初始化之后一切都很好。

When you reload the page the js app isn't loaded at first, so the browser sends the location in a new page load to the server. 重新加载页面时,首先不会加载js应用程序,因此浏览器会将新页面加载中的位置发送到服务器。 But because the server doesn't know about the angular app's routes, it tries to respond with it's own handler, which in this case doesn't exist. 但是由于服务器不知道Angular应用程序的路由,因此它尝试使用自己的处理程序进行响应,这种情况下该处理程序不存在。

The easiest way to fix this now is probably to change you ui-router config to include a # in the url. 现在解决此问题的最简单方法可能是更改ui-router配置,以在URL中包含#。

angular.module('app', ['ui.router'])
    ...snip
    $locationProvider.html5Mode(false);
});

That should start including a # before the angular part of the route. 应该在路由的角度部分之前开始包含#。 The new path would be "/#/sign_in". 新路径将为“ /#/ sign_in”。 The browser will know to send "/" before the # to the server, that would render the page with the angular init in int, then angular can handle the "/sign_in" part after the #. 浏览器将知道在#之前将“ /”发送到服务器,这将使用angular init呈现页面为int,然后angular可以在#之后处理“ / sign_in”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM