Reputation: 7043
I'm totally confused trying to route my app. I have a bunch of events, and each should have a photo gallery.
However I would like to keep everything within the same EVENT controller (btw, another question - how reasonable is that?). So the user can go on the Edit Event page and have a menu on the left side with links, one of which will be his gallery.
So I've added this to my event controller:
def gallery
@event = Event.find(params[:id])
end
The URI should be (I guess?): site/event/777/gallery/edit
How can I route that? And what will be the _path?
Thank you for any help
Upvotes: 0
Views: 190
Reputation: 38645
The following configuration in your config/routes.rb
should give you what you want:
resources :events do
resources :galleries
end
And this will give you event_galleries_path
. And, this will give you event_galleries_path
. Following are the seven paths the above configuration will provide:
event_galleries GET /events/:event_id/galleries(.:format) galleries#index
POST /events/:event_id/galleries(.:format) galleries#create
new_event_gallery GET /events/:event_id/galleries/new(.:format) galleries#new
edit_event_gallery GET /events/:event_id/galleries/:id/edit(.:format) galleries#edit
event_gallery GET /events/:event_id/galleries/:id(.:format) galleries#show
PUT /events/:event_id/galleries/:id(.:format) galleries#update
DELETE /events/:event_id/galleries/:id(.:format) galleries#destroy
The edit named route is: edit_event_gallery_path
.
Then instead of adding the gallery
method in your EventsController
, so you'd create your edit
, show
and other other actions in your GalleriesController
.
# /events/:event_id/galleries/:id/edit
def edit
@gallery = Gallery.find(params[:id])
end
# /events/:event_id/galleries/:id
def show
@event = Event.find(params[:event_id])
# And your galleries, something like this
@galleries = @event.galleries.find(params[:id])
end
Upvotes: 1
Reputation: 23939
I can think of no good reason to do this. Creating another file is trivial, have a GalleriesController with the usual show/edit/update/etc methods.
In your routes:
resources :events do
resources :galleries
galleries_controller.rb:
class GalleriesController < ApplicationController
# GET /events/1/galleries/1/edit
def edit
@event = Event.find(params[:event_id])
@gallery = @event.galleries.find(params[:id])
end
end
Upvotes: 2