`default_controller_and_action': missing :controller (ArgumentError) in Ruby 3 - ruby-on-rails

I am creating a website were users can post jobs and users can login,register etc. I have created my jobs model and created my user model for the login/register part. When I try to load rails server I keep getting this error below and cannot figure out what I'm doing wrong or how to fix. I originally was trying to use devise and create the users model but was having issues so I deleted the files for it. I am wondering if I deleted something or if I am missing something in my routes.rb file. Can someone help or point me in the right direction? I will post my routes.rb file as well. Thanks for any guidance as I am still new to rails. The only thing I added to the routes.rb file was root :to => "sessions#login" and below that. Im sure other info in this was added when i created the models and controller.
/home/whitey7/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/mapper.rb:181:in `default_controller_and_action': missing :controller (ArgumentError)**
Routes.rb
Application.routes.draw do
get "sessions/login,"
get "sessions/home,"
get "sessions/profile,"
get "sessions/setting"
get "users/new"
resources :jobs
root :to => "jobs#index"
root :to => 'home/index'
root :to => "sessions#login"
match "signup", :to => "users#new"
match "login", :to => "sessions#login"
match "logout", :to => "sessions#logout"
match "home", :to => "sessions#home"
match "profile", :to => "sessions#profile"
match "setting", :to => "sessions#setting"

I think you still have a route in routes.rb which in invalid. Please recheck all the routes, corresponding controller and action. Please also share the full error trace, that way we can pin-point the issue.

Please check if the jobs controller is still there. Because this is the first root directive in your routes declarations (and still, it does not make sense to have more than one), Rails is checking if this root route is available. It seems that the jobs controller is missing and causing this error.

At first, check Rails routing documentation. I think you are getting this error because you are unable to define route file. The problem I figure out in your route file are :-
a. You have three different root in your route file.
root :to => "jobs#index"
root :to => 'home/index'
root :to => "sessions#login"
b. You are defining same routes multiple times.
get "sessions/login,"
get "sessions/home,"
get "sessions/profile,"
get "sessions/setting"
get "users/new"
match "signup", :to => "users#new"
match "login", :to => "sessions#login"
match "home", :to => "sessions#home"
match "profile", :to => "sessions#profile"
match "setting", :to => "sessions#setting"
The solution might be as follows :-
a. Fix the root path at first. Which path you want to make root whether it is jobs index or home index or sessions login.
b. I think you are trying to define your routes as such
match "signup", :to => "users#new", via: :get
match "login", :to => "sessions#login", via: :get
match "home", :to => "sessions#home", via: :get
match "profile", :to => "sessions#profile", via: :get
match "setting", :to => "sessions#setting", via: :get

Related

Routes working fine on local but not in Production

I am currently trying to get my Ruby on Rails application running in a production environment. It is working fine but when I added a route under a nested namespace it is giving me error saying
ActionController::RoutingError (uninitialized constant Agent::Clients::AccountController)
This routes are working fine on my local machine, The route looks like this
namespace :agent do
root :to => redirect('url')
match 'dashboard', :to => 'dashboard#index'
match 'account', :to => 'account#edit'
match 'account/update', :to => 'account#update'
namespace :clients do
root :to => redirect('url')
**# This part I added and is giving routing error**
match 'accounts/invite', :to => 'clients/account#invite'
match 'accounts/sendinvite', :to => 'clients/account#send_invitation'
My rake routes giving the routes properly.
Any suggestions how to fix this issue ?
So you either want to do
match 'accounts/invite', :to => 'clients/accounts#invite'
match 'accounts/sendinvite', :to => 'clients/accounts#send_invitation'
Or
match 'accounts/invite', :to => 'agent/account#invite'
match 'accounts/sendinvite', :to => 'agent/account#send_invitation'

Not getting a [:id] parameter from url, Default route is not working

When I request a URL as follows:
http://localhost:3000/password_resets/edit/4RghIKJNygEDswIuuCo
I'm not getting the [:id] parameter, i.e. 4RghIKJNygEDswIuuCo.
Here is my route file, are there any modifications required for this?
ActionController::Routing::Routes.draw do |map|
match 'primary', :to => 'pages#primary', :as => "primary"
match 'admins', :to => 'admin_users#list', :as => "admins"
match 'login', :to => 'user_sessions#new', :as => "login"
match 'logout', :to => 'user_sessions#destroy', :as => "logout"
root :to =>"public#index"
match 'HFA/:id/' => 'public#show'
match 'HFA/:id/:uid' =>'public#show'
match 'public/projectview/:projectid/' => 'public#projectview'
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'
resources :users
resources :usertypes
resources :user_sessions
end
The default is /controller/:id/edit, following the REST architecture.
Do you really want to change this?
If so, verify the order of the declarations:
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.
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
and check your availables routes with the rake routes command:
http://guides.rubyonrails.org/routing.html#inspecting-and-testing-routes
if you put match instead of 'get' you will get all http (GET, POST, PUT, DELETE) route verbs
get 'password_resets/:id/edit', to: 'password_reset#create', as: :send_password_reset
the 'to:' defines the controller method, 'as:' defines the path name, 'get' defines the route verb and ':id' is the token created by this line in app/views/user_mailer/password_reset.text.erb
<%= send_password_reset_url(#user.password_reset_token) %>
run "rake routes" command and check the routes for "password_resets/edit/4RghIKJNygEDswIuuCo".

Adding Routes to Rails' Spree E-Commerce

I just recently upgraded to 1.0.3, and the routes.rb file in my config/routes folder seems to ignore all my custom routes.
MY routes.rb
JollyStore::Application.routes.draw do
# Mount Spree's routes
mount Spree::Core::Engine, :at => '/'
root :to => 'pages#index'
namespace :admin do
resources :wysiwygs
end
match 'about_us/', :to => "pages#about_us"
match 'services/', :to => "pages#services"
match 'raw_resources/', :to => "pages#raw_resources"
match 'contact_us/', :to => "pages#contact_us"
match 'privacy_policy/', :to => "pages#privacy_policy"
match 'return_policy/', :to => "pages#return_policy"
match 'refund_policy/', :to => "pages#refund_policy"
match 'cancellation_policy/', :to => "pages#cancellation_policy"
match 'delivery_shipping_policy/', :to => "pages#delivery_shipping_policy"
end
If I run bundle exec rake routes, it returns all the approriate routes. But when I try to reach that specific page, I get :
undefined local variable or method `about_us_path'
Or the same error for every link that is within my custom routes. Somehow my routes are being ignored. Does anyone know a way to circumvent this issue?
I encountered the same error and found this solution, which solved it by prefixing main_app, before each of my_paths/_urls. In my case, these were links used in one of the /override.rb files.
So, try: main_app.about_us_path.
You can add new routes in the Spree using following block in routes.rb file
Spree::Core::Engine.routes.prepend do
# Your new routes
end
For me prepend did not work.
for me draw did the work:
Spree::Core::Engine.routes.draw do
resources :orders, except: [:new, :create, :destroy] do
post :my_order, on: :collection
end
end

Ruby on Rails Routing changes not working

Recently I've tried to update my routes to be more specific in the routing.rb file:
resources :users
match '/signup', :to => 'users#new'
I removed the resources :users and changed the above to
match 'users/new' => 'users#new'
match 'users/show/:id' => 'users#show', :as => :users_show
match 'users/edit' => 'users#edit'
match '/signup', :to => 'users#new'
But when I try to click on the same link that I've had before in my layout page using the signup_path (http://localhost:3000/signup), I get the following error:
undefined method `users_path' but I am not using users_path anywhere
Shouldn't it still work? I haven't changed anything else in the other pages. The controller and actions are still the same.
Thanks!
What it tells is that you don have helper method named "users_path", which is created by resources in routes.rb.
Check by yourself with rake routes, what you get with your example:
users_new /users/new.(:format) {:controller=>"users", :action=>"new"}
users_show /users/show/:id(.:format) {:controller=>"users", :action=>"show"}
users_edit /users/edit(.:format) {:controller=>"users", :action=>"edit"}
(and no, you don't need forward slash at the beginning of path).
You defined only routes for some pages (show/new/edit), but you don't have path for index/create/update/destroy actions. Add resource back to routes.rb and check which routes it generates. Remember that by default your match-ed routes match all requests, not specific HTTP methods (like with resource). If you want to fully reassemble this behavior, take a look at documentation (at http://api.rubyonrails.org search for "match") and :via parameter.
Also why do you want to remove resource, and add all paths by yourself? If you want to limit available routes, then there are :only/:except options, you can also pass block and add routes by yourself, just check documentation (at http://api.rubyonrails.org and search for "resources" - it's shame I no longer can get link to specific documentation page and from Rails documentation).
I think basicxman is on the right track. You need a leading '/' in all your routes
Change:
match 'users/new' => 'users#new'
match 'users/show/:id' => 'users#show', :as => :users_show
match 'users/edit' => 'users#edit'
To:
match '/users/new' => 'users#new'
match '/users/show/:id' => 'users#show', :as => :users_show
match '/users/edit' => 'users#edit'
Also, do you still have the resources :users line at the top of your routes.rb? That'll also be necessary.
You have to add a route to create action as well. Probably your signup form points to users_path with POST method which is basically the create action. So add to your routes the following
match 'users' => 'users#create', :via => :post, :as=>:users
For more customization info please see the Rails routing guide

understanding rails routes: match vs root in routes.rb

i am following a rails tutorial from this link:
http://ruby.railstutorial.org/chapters/filling-in-the-layout#code:static_page_routes
in the /config/routes.rb file, i have
SampleApp::Application.routes.draw do
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'
end
when i run the site, it gives me an error: no route exist pages/home. i search around the forum and ppl suggest putting match '/pages/home' => 'pages#home'
which i did:
SampleApp::Application.routes.draw do
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/pages/home' => 'pages#home'
root :to => 'pages#home'
end
everything works. but now, my question is, what is the difference between
1. match '/something', :to => 'pages#something'
2. match '/something' => 'pages#something'
3. root :to => 'pages#home'
basically, the code i just put. shouldn't the root takes take of the main home page and i wont' need match pages/home => pages#home?
so confusing,
Thanks!
EDIT1: I'm not getting the answers I want and so I assume my question is wrong. I'll break it down into 2 parts:
What is the difference between:
match '/pages/home' => 'pages#home'
AND
root :to => 'pages#home'
some say that root takes it to your root page which i can understand but as i explained above, if i just have root to: the pages/home shows a routing error. pages/home should be the same as the root page, correct?
what is the difference between:
match '/contact', :to => 'pages#contact'
AND
match '/pages/home' => 'pages#home
syntactically, the first line has the :to => and the 2nd line does not. is the to: needed? what does it do?
thanks
As far as I know
match '/something', :to => 'pages#something'
match '/something' => 'pages#something'
are equivalent. It isn't uncommon to find more than one way to say the same thing in Rails. Shorthand notation abounds for commonly used methods. If you care, the latter is what I use and see more often.
As far as the root route is concerned, here is what is going on: root :to => 'pages#home' is mapping "/" to the home method in pages_controller.rb, as you already know. But using "pages#home" does not create the url "pages/home". All it does is tell rails what to execute when it encounters "/". That is why you need to also tell rails what to do when it encounters "pages/home". Route definitions are a one-way deal.
There is a lot more I could say, but I will try to keep my answer brief. Let me know if you need more clarification. Also, this rails guide is a great resource.
root :to => 'pages#home'
The url / will be mapped to pagescontroller home action.
/something will be the url mapping for the pagescontroller's something action
root :to => "pages#home"
is the default route, i.e. when you go to "yourdomain.com/" it routes to the home action in the pages controller.

Resources