acacia
acacia

Reputation: 1387

Remove controller from friendly URL

I have written freindly URLs for the show action of the School Resource but now have

before i had ;

http://webaddress/schools/2

and now i have;

http://webaddress/schools/school_name

However, i want

http://webaddress/school_name

My config routes look like this for the resource;

resources :schools do
  collection do
    match 'search' => 'schools#search', via: [:get, :post], as: :search
  end

end

How can i achieve that? thank you

Upvotes: 0

Views: 62

Answers (2)

Richard Peck
Richard Peck

Reputation: 76784

A more conventional way would be to use the path: option in your route resources, like this:

#config/routes.rb
...
resources :schools, path: "", only: :show #-> has to go at end of file!

This will give you the ability to add different methods to this, as well as keeping with Rails conventions :)

Upvotes: 0

Mohit Jain
Mohit Jain

Reputation: 43959

Add this at the last your routes file:

match ':id' => 'schools#show', via: [:get]

Upvotes: 2

Related Questions