41

I am getting

$ rake routes
rake aborted!
ArgumentError: Missing :action key on routes definition, please check your routes.
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:243:in `default_controller_and_action'
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:117:in `normalize_options!'
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:65:in `initialize'
/usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/routing/mapper.rb:1487:in `new'
/usr/local/r................

Here is my Routes.rb

Rails.application.routes.draw do
  get 'script/index'

  get 'landing/index'

  root 'landing/index'
end

What is causing the problem and how do I fix it.

Tithos
  • 1,191
  • 4
  • 17
  • 40

5 Answers5

57

The Rails router recognizes URLs and dispatches them to a controller's action. The error is caused by missing out the mapped action.

Rails.application.routes.draw do
  #   url               action
  get 'script/index' => 'script#index'
  get 'landing/index' => 'landing#index'
  root 'script#index'
end
notapatch
  • 6,569
  • 6
  • 41
  • 45
Kaleidoscope
  • 3,609
  • 1
  • 26
  • 21
  • Basically your defining both how the root will look (left) and how rails will actually find it (right). – tfantina Nov 04 '16 at 22:20
  • 1
    Important: Use **#** symbol at the **right** side. (This was the error reason in my case.) – Beauty May 28 '17 at 14:25
13

You can do it many ways, these all work:

  • get 'script/index'
  • get 'script/index' => 'script#index'
  • get 'script/index', to: 'script#index'

Think of path first and controller#method to follow.

Root is a special case, always: root 'script#index'

rtfminc
  • 6,243
  • 7
  • 36
  • 45
6

Change root 'landing/index' to root 'landing#index'

icedTea
  • 495
  • 2
  • 7
  • 12
1

I had the same error running rails g.

If you run a command that uses routes.rb, the file needs to be error free for the command to work.

In your case, you had paths, but you didn't match them to actions, so the routes.rb file was broken. You needed something like get 'landing/index' => 'my_controller#my_action'

Mirror318
  • 11,875
  • 14
  • 64
  • 106
0

Kaleidoscope's code works just fine. Below is a slightly concise version.

Rails.application.routes.draw do
  get 'script/index'
  get 'landing/index'
  root 'script#index'
end

Rails add the left side of the arrow(=>) by convention replacing / with #.

James Parker
  • 457
  • 4
  • 19