0

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?

jkeuhlen
  • 4,401
  • 23
  • 36
hellion
  • 4,602
  • 6
  • 38
  • 77
  • Did you get this answered? Could you post your controller as well? – jkeuhlen Jul 14 '14 at 17:23
  • I haven't found the"rails way" I was looking for. My controller isn't important in this questions...and this affects every controller. The way I have adapted @jkeuhlen block suggestion is to create an array of all models then iterating over that array and converting the string value in the array to a symbol and build a dynamic history route resource for each model in the array. Hope that makes sense. Its a bit cleaner...but, not the "rails way" I hoped to find. – hellion Jul 16 '14 at 01:47

1 Answers1

1

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.

Community
  • 1
  • 1
jkeuhlen
  • 4,401
  • 23
  • 36
  • This is a bit cleaner than nesting the history resource inside a block for each route. I haven't been able to find anything cleaner...sure seems like rails would have a cleaner way to do this. Thanks. – hellion Jul 16 '14 at 01:41
  • 1
    I think this is the only 'rails way' to do this. It's messy when you have 10+ resources but its better than having each block separately. – jkeuhlen Jul 16 '14 at 13:12