Rails和Angular Routing(Rails and Angular Routing)

编程入门 行业动态 更新时间:2024-10-19 17:43:58
Rails和Angular Routing(Rails and Angular Routing)

所以我有一个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.

更多推荐

本文发布于:2023-08-07 02:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rails   Angular   Routing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!