用于将模型与父级关联的基本 Rails 模式(嵌套?)

发布于 2024-10-08 04:00:15 字数 965 浏览 4 评论 0原文

我是 Rails 新手,试图了解如何使用 Rails 中可用的工具构建“复杂”模型(及其关联)。快速想象以下示例场景:用户是 ONE racing Team 的成员。用户可以创建汽车时间表。这些汽车有驾驶员燃油级别、发动机车轮。这些车轮轮胎轮毂MilesDriven。你明白了...

考虑到所有关于嵌套仅 1 层深度的警告...我仍然在努力解决如何实现 RESTful 和 Rails-y 并呈现一个允许用户构建一个 UI带轮胎汽车

我知道我们有一个Session &我们可以使用 Cookie 以及隐藏字段。因此用户登录...并被路由到他们的团队页面teams(current_user.team_id)。然后他们想要创建一辆汽车。这条路由到 new_team_car_path(current_user.team_id) 他们可以在那里建造一辆汽车...现在我想给这辆车添加一个轮子...这条路由到 new_team_car_wheel_path(current_user. team_id,car_id),等等,等等...?我认为不是……但是,铁路方式是什么?

此外,由于构建的所有内容最终都将与团队(和用户)相关联,因此最好将 team_id 和/或 user_id 关联一直向下“携带”到Tire 通过在运行时沿着层次结构向上关联来查询Team是否合理?

我确信这是基本的东西,但新手让我对如何最好地处理它感到困惑......

I'm new to Rails and trying to understand how we build 'complex' Models (and their associations) using tools available to us in Rails. Quickly imagine the following example scenario: Users are a member of ONE racing Team. Users can create Cars and Schedules. These Cars have Drivers, Fuel levels, Engines and Wheels. These Wheels have Tires, Hubs and MilesDriven. You get the idea...

Given all the warnings about nesting only 1 level deep...I still struggle with how to be RESTful and Rails-y and present a UI that allows a user to build a Car with Tires.

I know we have a Session & Cookie at our disposal as well as Hidden fields. So user logs in...and is routed to their team page teams(current_user.team_id). Then they want to create a Car. this routes to new_team_car_path(current_user.team_id) where they can build a car... now I want to add a Wheel to this car... so does that route to new_team_car_wheel_path(current_user.team_id, car_id), etc, etc...? I think not... But, what is the Rail-y way?

Also, as everything built will be associated, ultimately, to a team (and user) is it best to 'carry' the team_id and/or user_id association all the way down to the Tire or is it reasonable to query the Team by following the associations up the hierarchy at runtime?

I'm sure this is basic stuff but newbness has me confused about how to best approach it...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缪败 2024-10-15 04:00:15

这个问题没有单一的答案。您提到使用会话变量和隐藏字段——这些都是可行的方法,并且必须根据具体要求做出决定。事实上,您正在努力寻找最佳答案,这是一个好兆头,因为它表明您愿意考虑其他选择。

在这种情况下,您有优势,因为您不需要在 URL 中传递用户信息或其关联,因为您的授权代码已使其在控制器中可用。因此,我会从参数中删除用户和团队 ID,并从 current_user 访问它们:

# GET /cars/:car_id/wheels/new

def new
  @car = Car.find(params[:car_id])
  @wheel = @car.wheels.build
end

# POST /cars/:car_id/wheels

def create
  @user = current_user
  @team = @user.team
  @car = Car.find(params[:car_id]
  @wheel = @car.wheels.build(params[:car][:wheel])
  ...
end

唯一的问题如果您需要允许用户访问另一个用户的Car。在这种情况下,应该使用隐藏的表单字段(或下拉菜单)来选择用户。相反,您可能需要进行授权检查以防止用户访问彼此的“汽车”

There's no single answer to this question. You mentioned using session variables and hidden fields -- those are viable approaches, and the decision will have to be based on the specific requirements. The fact that you are struggling to find the best answer is a good sign, in that it shows you're willing to consider alternatives.

In this case you have a leg up because you don't need to pass in the user information or its associations in the URL, since your authorization code already makes it available in th controller. So I'd drop the user and team id from your params and access them from current_user:

# GET /cars/:car_id/wheels/new

def new
  @car = Car.find(params[:car_id])
  @wheel = @car.wheels.build
end

and

# POST /cars/:car_id/wheels

def create
  @user = current_user
  @team = @user.team
  @car = Car.find(params[:car_id]
  @wheel = @car.wheels.build(params[:car][:wheel])
  ...
end

The only problem is if you need to allow a user to access another user's Car. In that case a hidden form field (or a drop down menu) to select the user should work. Conversely, you may need an authorization check to prevent users from accessing each other's `Car'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文