Pablo Fernandez
Pablo Fernandez

Reputation: 287540

Is it possible to change the default action for a RESTful resource?

In Ruby on Rails, is it possible to change a default action for a RESTful resource, so than when someone, for example, goes to /books it gets :new instead of the listing (I don't care if that means not being able to show the listing anymore)?

Upvotes: 2

Views: 1204

Answers (5)

Ryan Bigg
Ryan Bigg

Reputation: 107728

def index
  redirect_to new_book_path
end

I think would be the simplest way.

Upvotes: 0

Mike H
Mike H

Reputation: 509

In the same vein, you can just do

def index
  show
end

Upvotes: 0

Milan Novota
Milan Novota

Reputation: 15596

Not sure why would you do such a thing, but just add this

map.connect "/books", :controller => "books", :action => "new", :conditions => { :method => :get}

to your config/routes.rb before the

map.resources :books

and it should work.

Upvotes: 3

Kevin Peterson
Kevin Peterson

Reputation: 7297

I'd point out that if you are pointing /books to /books/new, you are going to be confusing anyone who is expecting REST. If you aren't working alone, or if you are and have other come on board later, or if you expect to expose an API to the outside, the REST convention is that /books takes you to a listing, /books/new is where you create a new record.

Upvotes: 3

The Who
The Who

Reputation: 6622

Yes. You should be able to replace your index method in your controller...



def index
  @resource = Resource.new
  # have your index template with they proper form
end

Upvotes: 0

Related Questions