2

I'm having trouble adding a slashed slug and nested routes.

If I have these routes:

resources :courses do
  resources :registrations
end

I have these URLs:

/courses/7
/courses/7/registrations

If I change to_param in Course.rb, I can get some slugs happening in the routes:

def to_param
  "#{id}-#{slug}"
end

This then gives me:

/courses/7-title-of-course
/courses/7-title-of-course/registrations

All good so far.

The problem I'm having is after looking at this http://www.miguelsanmiguel.com/2011/03/17/slug-that-slash:

How do I get this to work with nested resources:

Course.rb:

def to_param
  "#{id}/#{slug}"
end

Routes.rb

resources :courses, :constraints => { :id => /[0-9]+\/.+/ } do
  resources :registrations
end

URL:

/courses/7/title-of-course
/courses/7/title-of-course/registrations

If I set things up like that the Course route is fine but the registration routes are broken.

Any tips here?

1 Answer 1

2

Try adding constraints:

resources :courses, :constraints => { :id => /.*/ } do
  resources :registrations
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.