user2636180
user2636180

Reputation: 1

Rails redirect controller to root

I have a website with a controller named "Posts".

I would like to redirect "example.com/posts" to "example.com" as they display the same information.

I know this is done in the routes.rb file but after a few hours of searching I don't think I'll figure it out. Any help is appreciated, thanks!

I am using Rails 4.0 on Ruby 2.0

Upvotes: 0

Views: 748

Answers (2)

jameswilliamiii
jameswilliamiii

Reputation: 878

In your routes.rb file make sure the root :to is at the top of your code. Then you would need to assign a redirect for the GET request you want to always sent there:

root :to => 'posts#index'
get '/posts', to: redirect('/')

Upvotes: 0

Ross Joo
Ross Joo

Reputation: 180

You can read all about redirection in routes, here: http://guides.rubyonrails.org/routing.html#redirection

get '/posts', to: redirect('/')
...
root to: 'posts#index'

Upvotes: 2

Related Questions