嵌套路线的命名参数 [英] Naming params of nested routes

查看:85
本文介绍了嵌套路线的命名参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

resources :leagues do
  resources :schedule
end

这将生成:

leagues/:id
leagues/:league_id/schedule/:id

如何避免更改联赛ID?
所以它将是:

How can I keep the league ID from changing param names? So it'll be:

leagues/:id
leagues/:id/schedule/:schedule_id


推荐答案

不,请不要这样做。

之所以这样,是因为它为每个应用程序中的嵌套资源提供了一个通用接口。通过在应用程序中使其与众不同,您可以有效地抵御Rails。 Rails有一组严格的约定,您应该遵守。当您偏离这条路时,事情就会变得混乱。

The reason for it being this way is that it provides a common interface for nested resources across every single application. By making it different in your application, you're effectively going "against the grain" of Rails. Rails has a strict set of conventions that you should stick to. When you stray from this path, things get messy.

但是,如果您确实想在脚上开枪,隐喻地说,您将需要手动定义路由。这是控制器中七个标准动作的路线:

However, if you do want to shoot yourself in the foot, metaphorically speaking, you will need to define the routes manually. Here's the routes for the seven standard actions in a controller:

get 'leagues/:id/schedules', :to => "schedules#index", :as => "league_schedules"
get 'leagues/:id/schedule/:schedule_id', :to => "schedules#show", :as => "league_schedule"
get 'leagues/:id/schedules/new', :to => "schedules#new", :as => "new_league_schedule"
post 'leagues/:id/schedules', :to => "schedules#create"
get 'leagues/:id/schedule/:schedule_id/edit', :to => "schedules#edit", :as => "ed it_league_schedule"
put 'leagues/:id/schedule/:schedule_id', :to => "schedules#update"
delete 'leagues/:id/schedule/:schedule_id', :to => "schedules#destroy"

您可以看到,这很丑陋。但是,如果您真的真的想要这样做,那就是您要这样做的方式。

As you can see, it's quite ugly. But, if you really really really want to do it this way, that's how you'd do it.

这篇关于嵌套路线的命名参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆