Rails redirect controller to root - ruby-on-rails

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

You can read all about redirection in routes, here: http://guides.rubyonrails.org/routing.html#redirection
get '/posts', to: redirect('/')
...
root to: 'posts#index'

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('/')

Related

routing error for root and get pages

I am developing a rails app and This is my first time working with ruby or any server-side programming language. I connected to localhost:3000 and everything went well. I went to change the routing for my home page as well as create some new ones but I'm having an issue of gettign them to preview and I keep Getting error messages. Here is the snippet from my routes.rb file
My routes.rb
Rails.application.routes.draw do
root 'pages#home'
get 'about' => 'pages#about'
post 'forum' => 'pages#forum'
get 'contact-us' => 'pages#contact-us'
end
I'm still relatively new To programming as well as stack overflow so I can't embed images yet So it will only have a link. Any help would be appreciated. Thank you!
1 - Try to avoid fat arrow
2- Controller name should not contain -(dash) instead use _(Underscore) example(contact-us should be contact_us)
Rails.application.routes.draw do
root 'pages#home'
get '/about', to: 'pages#about', as: :about
post '/forum', to: 'pages#forum', as: :forum
get '/contact_us', to: 'pages#contact_us', as: :contact_us
end
3- After changing this run rake routes at your console in your project directory , you will get list of routes
4- you can now use routes helper of prefix_path example (root_path, about_path or forum_path, contact_path)

Rails: Remove Controller from URL

I've read a couple of posts, but I haven't found a clear solution.
I want to remove the controller from the URL. I'm getting this:
localhost:3000/pages/services
But I want this:
localhost:3000/services
This is my routes file right now:
Rails.application.routes.draw do
root 'pages#index'
get 'pages/services'
get 'pages/specials'
get 'pages/events'
get 'pages/about'
end
I suspect that I need to do something here, but I haven't been able to find a decent tutorial. Any suggestions?
Try this:
match 'services' => "pages#services", :as => :services
You can specify a name for any route using the :as option.
See here
I would just replace it with this:
Rails.application.routes.draw do
root '#index'
end

Why route root did not work?

I am following the tutorial Ruby on Rails Tutorials by Michael Hartl and try to finish the last step in section 3.4.4. When I change routes.rb to
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
end
and relaunch http://localhost:3000/static_pages , they said "We're sorry, but something went wrong". Then I relaunch http://localhost:3000/static_pages/help and it works. Then I check the routes by
rake routes
And the result shows:
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
static_pages_help GET /static_pages/help(.:format) static_pages#help
static_pages_about GET /static_pages/about(.:format) static_pages#about
I check the content in file static_pages_controller.rb and no difference as that in tutorial. Could someone tell me what is wrong?
Well, with this route mapping root 'static_pages#home', you said, when you hit http://localhost:3000/ url, you will be mapped to the home action of the StaticPages controller.
But you didn't define any mapping for http://localhost:3000/static_pages, it is incorrect url as per the route.rb file. That's why you got the error.
Read the first line of the output of rake routes, it is clearly telling what you have defined.
The line root 'static_pages#home' is equivalent to get '/', to: 'static_pages#home'. This means that the URL localhost:3000/ is being routed to the #home action on the StaticPages controller.
If you want localhost:3000/static_pages to point to the #home action, add this line:
get 'static_pages', to: 'static_pages#home'

Move one controller's path under root namespace

I copied the following pages in welcome controller
views/welcome/about.haml
views/welcome/brand.haml
views/welcome/brand_detail.haml
views/welcome/contact.haml
views/welcome/download.haml
views/welcome/faq.haml
views/welcome/news.haml
views/welcome/news_detail.haml
views/welcome/product.haml
views/welcome/product_detail.haml
Now I write my route in this way
get 'welcome/index'
get 'welcome/about'
It seems repeat the welcome many times and the namespace is not under root.
If I understand you correctly, you want the following in your config/routes.rb:
get 'about', to: 'welcome#about'
get 'brand', to: 'welcome#brand'
... and so on ...
root 'welcome#index'
Write this in your routes.rb file
root 'welcome#index'

Rails Routes Question

I have a really simple app I've built using RoR but I'm stuck modifying my routes.
It's basically a site which lists user information - I need to change the url from:
mydomain.com/users/user-1
to
mydomain.com/user-1
Update..
I've managed to route the above request using:
match "/:id", :controller=>"users", :action=>"show"
But what I really need to do is change the route for all requests to /users/# to /
Although my route is working, all my links to show a user still point to:
/users/user-#
--- Update ---
The routing for /user-id is now working perfectly however, I'm struggling with the rest of the routing now.
I can now navigate to http://localhost/user-1
However, I basically need to remove the /user/ part completely. When I'm editing / updating a page, I end up with it going to:
/users/user-1/edit
All works fine but it then redirects to"
/users/user-1/
I really need both of these to redirect to
http://localhost/user-1/edit
Thanks
Bob
You want:
resources :users, :path => '/'
I believe get ":id" => "users#show" will be much the same except you only allow HTTP GET. Hope this works.
At the bottom of your routes
match "/:id", :to => "users#show"
There is some side effects so be ready
to rewrite your routes you should specify its name:
match "/:id", :to => "users#show", :as => :user
or, as #Whirlwin pointed, better to use just GET request as default
get "/:id", :to => "users#show", :as => :user
So now you can call:
user_path(#user)

Resources