Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Rails routing situation

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.

like image 827
Adam Rezich Avatar asked Mar 02 '23 01:03

Adam Rezich


1 Answers

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', ...
like image 74
Terry G Lorber Avatar answered Mar 05 '23 14:03

Terry G Lorber