[英]Rails routes, using something other than model_id in resources route
I have some nested resources. 我有一些嵌套资源。 Here's an example:
这是一个例子:
resources :contests do
resources :scoring_periods do
resources :entries
end
end
I'd ultimately like to have a URL that looks like the following: 我最终希望拥有一个如下所示的URL:
/contests/1/scoring_periods/10/entries/new
The catch here is that the /10/ in scoring_periods is not the ScoringPeriod#id. 这里要注意的是scoring_periods中的/ 10 /不是ScoringPeriod#id。 It is instead another attribute named period_count in this case.
在这种情况下,它是另一个名为period_count的属性。 I'd like to be able to reference the period_count in the URl instead of the ID as my system might have millions of IDs later and it's just not intuitive to list it there.
我希望能够引用URl中的period_count而不是ID,因为我的系统以后可能有数百万个ID,并且在此处列出它并不直观。 The actual period_count number, makes a lot more sense to the users entering this contest.
实际的period_count编号对参加此竞赛的用户来说意义更大。
Is there a way to munge the resources entry in routes.rb in order to allow me to reference scoring_periods by an attribute other than :scoring_period_id
? 有没有办法在route.rb中修改资源条目,以便允许我通过
:scoring_period_id
以外的属性来引用:scoring_period_id
?
Something like this should work: 这样的事情应该起作用:
resources :contests do
scope path: '/scoring_periods/:period_count/' do
resources :entries
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.