I have some more complex routes setup in my app, that I'm wondering could be turned into resourceful routes.
What are the ideal Rails conventions for turning these into resourceful routes?
Route 1: /grandparent-place/parent-place/place/
These routes are at the bottom of my routes.rb file, as they pull from the root path and are scoped by parents and children.
Routes.rb
get ':grandparent_id/:parent_id/:id', to: 'places#show', as: :grandparent_place
get ':parent_id/:id', to: 'places#show', as: :parent_place
get ':id', to: 'places#show', as: :place
Places_Controller.rb
def set_place
if params[:parent_id].nil? && params[:grandparent_id].nil?
@place = Place.find(params[:id])
elsif params[:grandparent_id].nil?
@parent = Place.find(params[:parent_id])
@place = @parent.children.find(params[:id])
else
@grandparent = Place.find(params[:grandparent_id])
@parent = @grandparent.children.find(params[:parent_id])
@place = @parent.children.find(params[:id])
end
end
Application_Helper.rb
def place_path(place)
'/' + place.ancestors.map{|x| x.id.to_s}.join('/') + '/' + place.id.to_s
end
Route 2: /thread#post-123
These routes are meant to allow for only specific actions, using the parent module to specify the controller directory - and using a # anchor to scroll to the specified post.
Routes.rb
resources :threads, only: [:show] do
resources :posts, module: :threads, only: [:show, :create, :update, :destroy]
end
Application_Helper.rb
def thread_post_path(thread, post)
thread_path(thread) + '#post-' + post.id.to_s
end
Is it convention to override the route paths in the application helper, or is there a better way to generate the correct URLs without overriding the helpers?