所以我有一个rails应用程序,角度缓慢介绍。 IM有一个问题,轨道路由和角度ui路由很好地一起玩。
我有一个订单/ index.html.erb
<div ng-app="orders"> <div ui-view></div> </div>该ultimatley路由到partials/orders/users/index.html file 。 在该文件中,我有一个指向编辑页面的链接,如下所示。 <a ui-sref="users_orders_edit({userId: order.user.id, orderId: order.id})">Edit</a>
该链接通过角度路由到编辑页面。 这是问题所在。 当我使用url /users/:id/orders/:order_id/edit刷新编辑页面时,它会抛出一个rails错误,说没有定义路由。
我在这里想念的是什么?
So I have a rails app, with angular being slowly introduced. IM having an issue with rails routing and angular ui-route playing together nicely.
I have a order/index.html.erb that has
<div ng-app="orders"> <div ui-view></div> </div>That ultimatley routes to a partials/orders/users/index.html file. Within that file I have a link to the edit page, that looks like this.<a ui-sref="users_orders_edit({userId: order.user.id, orderId: order.id})">Edit</a>
That link is being routed by angular to an edit page. Here is the issue. When I refresh the edit page with the url /users/:id/orders/:order_id/edit, it throws a rails error, saying that route is not defined.
What am I missing here?
最满意答案
这就是我要解决的问题。 在routes.rb文件中,在文件底部添加以下内容,使其与每个路径不匹配:
get "/(*redirect_path)", to: "the_controller_that_serves_your_angular_app#index"您不必将其称为redirect_path。 它可以任意命名,因为你不会使用它。 这告诉rails将所有不确定的路径映射到您的角度应用程序。 这将允许您的用户刷新并仍然加载角度应用程序,甚至可以进入正确的状态。
Here's what I do to solve this. In your routes.rb file add the following towards the bottom of the file so that it doesn't match every route:
get "/(*redirect_path)", to: "the_controller_that_serves_your_angular_app#index"You don't have to call it redirect_path. It can be named whatever you like because you won't use it. This is telling rails to map all uncertain paths to your angular app. This will allow your users to refresh and it still load the angular app and even go to the correct state.
更多推荐
发布评论