Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7631211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:17:51+00:00 2026-05-31T06:17:51+00:00

I know how to setup nested resources in the routes files already… the question

  • 0

I know how to setup nested resources in the routes files already… the question is for how to do it optionally with the same payload, and with fewer lines.

Lets say I have a BlogSite. BlogSite has many Posts, but it also has many Authors and many Dates. (this might not be the best example, but bear with me).

To do CRUD on a Post, I want to be able to use

/blog_sites/1/author/2/date/3/posts #all posts on site 1 from author 2 on date 3
/blog_sites/1/author/2/posts        #all posts on site 1 from author 2
/blog_sites/1/date/3/posts          #all posts on site 1 on date 3
/blog_sites/1/posts                 #all posts on site 1
/author/2/date/3/posts              #all posts from author 2 on date 3
/author/2/posts                     #all posts from author 2
/date/3/posts                       #all posts from date 3
/posts                              #all posts 

Such that any of the filtering parameters can be optional in the URL. I know that you can use something like

get (/blog_sites/:blog_id)(/author/:author_id)(/date/:date_id)/posts => "posts#index"

but I don’t want to lose all the CRUD benefits of using nested resource routing. Currently I have to duplicate much of the routing to make it work, and am looking for a better way to do this:

resources :blog_sites do
    resources :authors do
        resources :dates do
            resources :posts
        end
        resources :posts
    end
    resources :dates do
        resources :posts
    end
    resources :posts
end

… and so on. It can get pretty unmanageable petty quickly.

How can I maintain the optional param urls whilst keeping routes.rb DRY and sane?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:17:53+00:00Added an answer on May 31, 2026 at 6:17 am

    Try to use scope and resources together. Rails 3 routing with resources under an optional scope

    scope 'blog_sites/:blog_id)(/author/:author_id)(/date/:date_id)' do 
        resources :posts
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(if the question is more appropriate for RackOverflow please let me know) I've setup
Does anyone know how you setup new commands to launch an application from the
I would like to know how to setup an SVN repository with LDAP authentication.
I'd like to know how to setup my exim4 on Debian so that it
Does anybody out there know how to setup Eclipse with MinGW or Cygwin to
Has anyone used this plugin? I don't know if my setup is right, but
just curious if you know of any way to setup a drag boundary for
I'm trying to setup some friendly URLs on a SharePoint website. I know that
I have the following setup, and I need to know how to persist state.
I know how to setup an alarm to fire repeatedly, but the alarm stops

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.