您的以下配置config/routes.rb
应该可以满足您的需求:
resources :events do
resources :galleries
end
这会给你event_galleries_path
。而且,这会给你event_galleries_path
. 以下是上述配置将提供的七个路径:
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
名为路由的编辑是:edit_event_gallery_path
.
然后,不要在您的,中添加gallery
方法,而是在您的.EventsController
edit
show
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