Ashenafi Abera
Ashenafi Abera

Reputation: 91

Missing :controller key on routes definition, please check your routes

I just started learning Ruby on Rails and got stamped on Chapter 7 of Beginning Rails 4 Third Edition Page 151.

I generated controller as follows; $rails generate controller ControllerName [actions] [options]. This one worked OK but when I tried to generate a controller for the Users

$rails generate controller Users. I got the following error message. /routing/mapper.rb:328:in check_part : Missing :controller key on routes definition, please check your routes. (ArgumentError).

This is what my routes look like

Rails.application.routes.draw do                                    
get 'controller_name/[actions]'
get 'controller_name/[options]
root :to => "articles#index"
resources :articles
root :to => 'users#show'

end**

I added the last route (root :to => 'users#show')

Stackoverflow community is great. I have been getting a lot of help from the archives.

Thanks

Upvotes: 9

Views: 21228

Answers (3)

therealrodk
therealrodk

Reputation: 434

The main problem is that you need # instead of / in your routes.

Another problem you have is that root tells your app where to send users when they arrive at the homepage, ie, the root directory of your app. Therefore, you can only specify one action for the root.

Instead of:

Rails.application.routes.draw do                                    
  get 'controller_name/[actions]'
  get 'controller_name/[options]
  root :to => "articles#index"
  resources :articles
  root :to => "users#show"
end

Do this:

Rails.application.routes.draw do 
  root :to => "articles#index"
  # Or
  root :to => "users#show"

  get 'controller_name#[actions]'
  get 'controller_name#[options]

  resources :articles
end

Upvotes: 18

puneet18
puneet18

Reputation: 4427

Correct command to generate controller with actions:

rails generate controller ControllerName [actions] [action2] ...

without actions

rails generate controller ControllerName

And in routes there is only one root url.

Upvotes: 0

James Milani
James Milani

Reputation: 1943

Your problem is in the rails g controller ControllerName [actions] [options].

This will generate routes and helpers, spec files, and all sorts of things. The problem is that the [options] and [actions] will become method names in your controller, which aren't valid ruby method names, and the route get 'controller_name/[options]' is not a valid route. Try deleting the routes:

get 'controller_name/[actions]'
get 'controller_name/[options]'

That should clear your error.

In general, you want your routes to have the form:

method 'path/to/route', to: 'controller#action'

e.g.
get '/my_new_api', to: 'api#my_new_api', as: :my_new_api
# Sets a route of a GET request to '/my_new_api' referencing
# the my_new_api method in the api controller.

This why you were getting the error that you were getting, you route wasn't properly specifying a controller.

Here's a link to Rails routing. You can see the basic form of routes there.

But ultimately, to solve your problem will probably want to run a rails destroy command:

rails d controller ControllerName [actions] [options]

Or delete the erroneous files from the command line.

They'll be easy to see if you are using git and will look something like this with a git status:

Untracked files:
  (use "git add <file>..." to include in what will be committed)

app/assets/javascripts/controller_name.js
app/assets/stylesheets/controller_name.scss
app/controllers/controller_name_controller.rb
app/helpers/controller_name_helper.rb
app/views/controller_name/
spec/controllers/
spec/helpers/
spec/views/

Welcome to SO! :-)

Oh and here is a link to the docs for rails generate commands.

Also, conveniently, you can just type rails generate on the command line and it will show you lots of options, and you can type rails generate controller for more specific options and examples about controllers.

Upvotes: 3

Related Questions