I'm creating a Ruby on Rails app that consists of stories. Each story has multiple pages.
How can I set up routes.rb so that I can have URLs like this:
http://mysite.com/[story id]/[page id]
Like:
http://mysite.com/29/46
Currently I'm using this sort of setup:
http://mysite.com/stories/29/pages/46
Using:
ActionController::Routing::Routes.draw do |map|
map.resources :stories, :has_many => :pages
map.resources :pages
map.root :controller => "stories", :action => "index"
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
Thanks in advance. I'm a newbie to Rails and routing seems a bit complicated to me right now.
Here's a good resource.
map.connect 'stories/:story_id/:page_num', :controller => "stories", :action => "index", :page_id => nil
In your controller:
def index
story = Story.find(params[:story_id])
@page = story.pages[params[:page_num]]
end
UPDATE: Just noticed you don't want 'stories' to appear. Not sure if dropping that from the map.connect will work... try it out.
map.connect '/:story_id/:page_num', ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With