Sometimes getting an "Invalid Route Name" Error in Rails - ruby-on-rails

I am getting the following error only on occasion when I attempt to load up my Rails app in localhost.
Invalid route name, already in use: 'root' You may have defined two routes with the same name using the :as option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with resources as explained here: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
For some reason, it only happens every now and again, and generally goes away after I refresh the page once. The file it is calling into question has this as the code (line causing the error indicated):
Rails.application.routes.draw do
get 'welcome/index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
Rails.application.routes do
resources :articles
root 'welcome#index' #-->This is the line that causes the error
end
resources :articles do
resources :comments
end
end
I am really new to Rails, and I'm not sure what is causing the problem or if would even be a problem if I were to actually host this beginner project on the web. Also, if it helps, I am running Ruby 2.2.2 and Rails 4.2.1.
Thank you in advance for the help!

You have (Rails.application.routes) nested inside (Rails.application.routes.draw). Run rake routes and you will see that you have resources for articles twice. Furthermore, you have root 'welcome#index' nested inside and that is why you are getting the error. Your routes should look like this
Rails.application.routes.draw do
get 'welcome/index' => 'welcome#index'
root 'welcome#index'
resources :articles do
resources :comments
end
end
Note that the route of your application meaning(/) and (/welcome/index) both point to the welcome controller index action. You don't really need get 'welcome/index' => 'welcome#index' and you can delete that line and use root when ever you need the index action from the welcome controller.

Try using the "Full" syntax.
root :to=> "some#action"

You have two routes pointing to the same page index, get rid of your get route
get 'welcome/index'
Also your root route is nested that should be moved outside since the root route is the root of your entire app and not a specific resource

Related

Can't deploy app on heroku, always get "The page you were looking for doesn't exist." error

Hi I'm trying to deploy an app made with help of "Agile web development by S.Ruby" and I always get the same error - The page you were looking for doesn't exist.
You may have mistyped the address or the page may have moved.
I've already tried to migrate my DB on Heroku - that wasn't the case.I think something is wrong with routes.rb file, but I can't understand what is incorrect exactly,please help me to solve this problem
Here is my routes.rb file:
Depot::Application.routes.draw do
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
get "sessions/create"
get "sessions/destroy"
resources :users
resources :products do
get :who_bought, on: :member
end
scope '(:locale)' do
resources :orders
resources :line_items
resources :carts
root 'store#index', as: 'store', via: :all
end
end
As Michal correctly points out you miss the root path. You have defined a route inside the scope you use to get to the different locales, but not a global root. This is not a Heroku problem, it won't work on your local server either.
So, http://your_server.com/en will work, but http://your_server.com will not.
You need to add a root path outside all scopes, like so:
root 'store#index'
You will have to set a default locale or something like that. You can leave the other root directive inside the scope, as you have named it explicitly (with as: 'store') there won't be any conflict.

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

Route appears in rake routes, but not on site

I'm new to ruby on rails, and I'm having trouble getting up to speed. I've set up devise for user authentication, and everything seemed to be going well. Until I tried to get routes set up
Basically, I've deleted my /public/index.html, and I've set a new default route. Here's my /config/routes.rb
MyApp::Application.routes.draw do
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
end
Okay, so that seems simple enough. I generated a home controller/view. My index.html.erb is just the default. My controller is also blank.
When I run rake routes, I can see the route to the root.
root / home#index
root / home#index
So this seems like a go! But...It's not. I get an error when I try and open my index
No route matches [GET] "/"
I don't feel like the details of my server should be significant, but it's nginx. I can post my server configuration if anyone thinks it's important.
Blargh. I'm an idiot. The server configuration I though wouldn't matter was the problem. I had edited my nginx settings, but I left the default in Unicorn. I never noticed before now, since the /public/index.html in both the unnecessary app and my app were the same. Thanks for all the help though.
You should be add resources :homes
MyApp::Application.routes.draw do
resources :homes
root :to => "home#index"
devise_for :users
end

Rails Routing Error 'no route matches'

I'm getting this routing error on my site. I've went through other related questions and haven't found a solution yet.
No route matches {:action=>"show", :controller=>"businesses", :user_id=>3}
I have three models: user (contains login details), business (contains info related to users: one user has_one business), and business_hours (one business has many business_hours).
Here are my routes:
devise_for :users
get "home/index"
root :to => "home#index"
resources :users do
resources :businesses do
resources :business_hours
end
end
Edit:
I get the error just trying to access the home page (localhost:5000). I'm currently signed in. I have run rake routes and the route seems to be there:
user_business GET /users/:user_id/businesses/:id(.:format) businesses#show
the routes which you have mentioned is expecting a forth argument which is the id of the businesses,try to pass the id of businesses it will work
user_business GET /users/:user_id/businesses/:id(.:format)
ideally you need to use this for index
user_businesses GET /users/:user_id/businesses(.:format)
your question is not so clear otherwise i could have given a precise answer hope this helps

Custom actions in rails controller with restful actions?

I'm having some trouble with using creating my own actions inside a controller I generated using the scaffold.
I understand everything maps to the restful actions but I'm building a user controller where users can login/logout, etc but when I created the action and declared it in the routes.rb I get this error when I visit users/login
Couldn't find User with id=login
It tries to use login as a ID parameter instead of using it as an action.
Routes.rb
match 'users/login' => 'users#login'
I think I'm doing something wrong with the routes so if anybody could help me that would be great.
Thanks
I assume your routes.rb looks like this:
resources :users
match 'users/login' => 'users#login'
The problem is that Rails uses the first route that matches. From the documentation:
Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action’s route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.
So either define your custom route before resources :users:
match 'users/login' => 'users#login'
resources :users
…or use this syntax for adding more RESTful actions:
resources :users do
collection do
match 'login'
end
end
To see the existing routes (and their order) run rake routes from the command line.

Resources