Wasi
Wasi

Reputation: 1246

Routing problem in ruby on rails

I am new to ruby on rails. I used the command 'rails generate controller Courses new'

Then, I edited routes.rb file with:

  resources :courses
  match '/courses', :to => 'courses#new'

When I access http://0.0.0.0:3000/courses. I get an error:

Unknown action

The action 'index' could not be found for CoursesController.

I think i am missing something. Please help

Thanks.

Upvotes: 0

Views: 298

Answers (3)

chris finne
chris finne

Reputation: 2331

Put your "match" line before your "resources" line.

Upvotes: 0

Ryan Bigg
Ryan Bigg

Reputation: 107708

You could use this instead:

resources :courses, :except => :index
match '/courses', :to => 'courses#new'

The except option takes a symbol or an array of actions in the controller you do not want to define resource routes for. In this case, we turn off the route for the index action, /courses/.

Next, we define the same route that would have been defined for the index action, but point it at CoursesController#new.

Upvotes: 0

rmk
rmk

Reputation: 4455

The line

resources :courses generates the routes for courses like so:

/courses -> coursescontroller#index

/courses/:id -> coursescontroller#show
...

and so on. This is known as 'restful routes'.

If you do not want to direct a url of form 'courses.html' to the 'index' action of your courses controller, but to the 'new' action of your courses controller (which would be highly unusual, by the way), just remove the first line from your routes.rb.

If you want to see what routes you have defined, just do

rake routes

from your rails app directory.

Upvotes: 1

Related Questions