views:

53

answers:

2

Hello. I have a resource called Book, then I have domains like:

   domain.com/books/272

But I want to change it to

   domain.com/stories/272

Only for the URL, don't need to change controller, classes etc.

In the routes I have

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

  map.root :controller => 'static'

How can I do it? Thanks

+3  A: 

Depends really on what you have already.

#resource routes
map.resources :books, :as => :stories
#named routes
map.books 'stories/:id'

Without defining routes the only option I can think of - which seems terribly wrong - is to add a new controller which inherits from your books controller. You'd need to go through your application and change the controller name used to generate paths or urls.

class BooksController < ApplicationController

class StoriesController < BooksController

Personally I would recommend you take the time to define your routes but I guess this depends on how large an application you're working with.

http://guides.rubyonrails.org/routing.html

mark
I posted my routes.rb as an edit, thanks
Victor P
Updated my answer.
mark
A: 

Its called named routes and is done in your config/routes.rb

In your routes file:

map.stories 'stories/:id', :controller => 'books', :action => 'show'

Then in your view you can access this route with:

<%= link_to book.name, stories_path(book) %>

Make sure you change book.name to whatever name you want. also make sure you passing book as a local variable to the routes path.

You can also change the :id to be more SEO friendly with to_param in the respective model.

In your model:

 def to_param
     "#{id}-#{name.gsub(/\s/, '_').gsub(/[^\w-]/, '').downcase}"
 end

Also make sure you replace name with an attribute that the book model actually has.

Sam
There's a much better way to do this by using the `:as` option.
Ryan Bigg
ok thanks for the info.
Sam