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 6778103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:13:38+00:00 2026-05-26T16:13:38+00:00

Is there any way to DRY up these routes. There is a pattern to

  • 0

Is there any way to DRY up these routes. There is a pattern to them:

get "articles/new" => "articles#new", :as => :new_article
post "articles/new" => "articles#create", :as => :create_article
get "articles/:slug/edit" => "articles#edit", :as => :edit_article

get "stores/:id/articles/new" => "articles#new", :as => :new_store_article, :defaults => { :scope => 'store' }
post "stores/:id/articles/new" => "articles#create", :as => :create_store_article, :defaults => { :scope => 'store' }
get "stores/:id/articles/:slug/edit" => "articles#edit", :as => :edit_store_article, :defaults => { :scope => 'store' }

get "warehouses/:id/articles/new" => "articles#new", :as => :new_warehouse_article, :defaults => { :scope => 'warehouse' }
post "warehouses/:id/articles/new" => "articles#create", :as => :create_warehouse_article, :defaults => { :scope => 'warehouse' }
get "warehouses/:id/articles/:slug/edit" => "articles#edit", :as => :edit_warehouse_article, :defaults => { :scope => 'warehouse' }

Thanks in advance!

  • 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-26T16:13:39+00:00Added an answer on May 26, 2026 at 4:13 pm

    I wanted an elegant solution and I seem to have found one. Basically, adding a helper method that I can use in my routes file by putting this in lib/routes_helper.rb:

    class ActionDispatch::Routing::Mapper
      def article_resources_for(scope = nil)
        scope_path_symbol = scope_path = nil
        defaults = {}
    
        unless scope.blank?
          scope_path = "#{scope}/:id/"
          scope_path_symbol = "#{scope}_"
          defaults = { :defaults => { :scope => scope } }
    
        get "#{scope_path}articles/new" => "articles#new", { :as => :"new_#{scope_path_symbol}article" }.merge(defaults)
        post "#{scope_path}articles/new" => "articles#create", { :as => :"create_#{scope_path_symbol}article" }.merge(defaults)
        get "#{scope_path}articles/:slug/edit" => "articles#edit", { :as => :"edit_#{scope_path_symbol}article" }.merge(defaults)
    
      end
    end
    

    Then in my routes.rb file, I can simply just do

    article_resources_for
    article_resources_for "stores"
    article_resources_for "warehouses"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

there any way to get my personal email accounts created in the iphone settings
Is there any way to open a new window with a specified URL using
Is there any way in Notepad++ (or even with another tool) to change the
Is there any way I can set a formatter on models that will convert
Is there any way to use Google's API to retrieve a user's current zipcode
Is there any way to overload the > (greater than) operator, to be able
Is there any way we can fetch X509 Public Cetrificates using c# from AD
Is there any way to stop animation in iOS 3 ? I know about:
Is there any way to update nested documents by id or some other field?
Is there any way in which I can automatically convert a Custom Class Object

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.