Move one controller's path under root namespace - ruby-on-rails

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'

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)

Map routes into other routes in Rails

I'm having some troubles with routes in Ruby on Rails v5.2.0
Currently, I have a resource called users, so that I have a controller which takes actions (for example index) whenever I start my server in localhost on port 3000 and type in my browser
localhost:3000/users/
Is there an easy way to map the requests for this resource to the app root?Basically, I'm trying to achieve this:
localhost:3000/users/ --> localhost:3000/
localhost:3000/users/new/ --> localhost:3000/new/
This is how my routes.rb file looks like right now:
Rails.application.routes.draw do
devise_for :users
get 'landing/index'
get 'welcome/index'
resources :users
root to: 'landing#index'
end
Add the following lines to your routes.rb file
Change
root to: 'landing#index'
to
root "users#index"`
and add the line
get "/new" => "users#new"
Also if you want to learn more on routing, here is the link
http://guides.rubyonrails.org/routing.html
TLDR - Rails doesn't have a root model generator for routing
You can manually create the individuals routes
get :new, to: "users#new", as: "new_user"
...
However while using the rails generators resources you are just specifying a shorthand for
scope :model do
get :new, to: "model#new", as: "new_model"
...
end
You can checkout the rails guide to routing for more specifics on explicit creation
http://guides.rubyonrails.org/routing.html
HACKY SOLUTION
root to: "users#index", as: "users"
get :new, to: "users#new", as: "new_user"
post "/", to: "users#create"
scope ":id" do
root to: "users#show"
get :edit, to: "users#edit", as: "edit_user"
patch "/", to: "users#update"
...
end
It looks that what you want is to 'mute' users from the url. An option for this is to call path: '' on users like this:
Rails.application.routes.draw do
devise_for :users
get 'landing/index'
get 'welcome/index'
resources :users, path: '' # <-- HERE
root to: 'landing#index'
end
The value you give to path: is going to replace the resource name.
In this scenario users is being replaced with an empty string '', but it could be any other string.
This will remove users. However, you must consider that root to: 'landing#index AND users#index are both pointing to localhost:3000/
Without knowing your app, an option to solve this scenario, could be to have landing#index as root for gustes (not authenticated users) and users#index as a root for authenticated users.

how to change route with rails 4.2,1/ruby 2.2.1?

I'm attempting to change the route for an Rails app that I'm building. I'm using c9.io's IDE.
The route folder has the following code:
Rails.application.routes.draw do
get 'home/index'
end
When I change it to:
Rails.application.routes.draw do
get 'home#index'
end
So that I can route the index page to the home page it produces an error.
Do I need to change the code in my controller or view files?
I think you're trying to do this. You want to root the "/" page to the home controller index action?
Rails.application.routes.draw do
root to: 'home#index'
end

Namespaced Rails 'root to:' route not working as expected

I'm reading the Rails Guides on routes (Routes From The Outside In), and I saw the following:
You can also use root inside namespaces and scopes as well. For
example:
namespace :admin do
root to: "admin#index"
end
root to: "home#index"
I'm trying to replicate this to see how it works, so in my config/routes.rb file I've got the following code:
namespace :admin do
root to: 'users#index'
end
I expected to be able to visit 'localhost:3000/admin' and be directed to the users#index page, but instead I got the error message 'uninitialized constant Admin'.
Am I misunderstanding what the example code is supposed to do, or is there something wrong with what I wrote?
namespace :admin, would route you to the controller Admin::UsersConroller. If you want to route /admin to UsersConroller, you should use scope instead of namespace.
scope '/admin' do
root to: 'users#index'
end
You can read more about it here

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

Resources