randombits
randombits

Reputation: 48500

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:

/contests/1/scoring_periods/10/entries/new

The catch here is that the /10/ in scoring_periods is not the ScoringPeriod#id. It is instead another attribute named period_count in this case. 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. The actual period_count number, makes a lot more sense to the users entering this contest.

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 ?

Upvotes: 0

Views: 199

Answers (1)

jokklan
jokklan

Reputation: 3550

Something like this should work:

resources :contests do
  scope path: '/scoring_periods/:period_count/' do
    resources :entries
  end
end

Upvotes: 1

Related Questions