Search code examples
ruby-on-railsruby-on-rails-4routespaper-trail-gem

How to add a nested route resource to all existing routes


I am using a great gem called paper_trail. I have created a page called 'history' that will list the versions for any resource. My routes file has a nested resource for EVERY route...which is not DRY at all.

resources :users do 
  get "/history" => "pages#history", as: "history"
end

this route gives me users/1/history

resources :companies do 
  get "/history" => "pages#history", as: "history"
end

now I have companies/1/history

How can I make the /history work as a nested route for ALL routes without filling my routes file with a nested history path for every resource?


Solution

  • You can do these types of actions in a block to avoid repeating yourself over and over.

    resources :users, :companies do 
      get "history" => "pages#history", :on => :member
    end
    

    Some additional helpful information is available on this SO question.