简体   繁体   English

Rails路线,在资源路线中使用model_id以外的其他名称

[英]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.

相关问题 在路由中使用ID以外的其他内容 - Using something other than ID in routes Rails RESTful路线:覆盖params [:id]或params [:model_id]默认值 - Rails RESTful Routes: override params[:id] or params[:model_id] defaults Rails Routes:更改资源标识符路径名? 始终使用 params[:model_id] 而不是 params[:id] - Rails Routes: change resource identifier path name? always use params[:model_id] instead of params[:id] Rails Route 基于与 ID 不同的东西 - Rails Route based on something different than ID 在rails 3路由中使用:id以外的变量 - Use variable other than :id in rails 3 routes 覆盖Rails中的默认route / path helper以使用除`id`字段之外的其他内容 - Overriding the default route / path helper in rails to use something other than the `id` field 使用多个列建立关系时使用Rails关联(即model_name和model_id) - Using rails associations when using multiple columns to establish the relationship (i.e. model_name and model_id) 使用资源而不是Rails路由中的id生成永久链接 - generating permalink with resources rather than id in rails routes Rails关联使用'model'。'model_id'而不是'model'。'id'触发SQL where子句 - Rails association triggers SQL where clause with 'model'.'model_id' instead of 'model'.'id' 如何从模型获取model_id? - How to get model_id from Model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM