开发者

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 'ind开发者_如何学Goex' could not be found for CoursesController.

I think i am missing something. Please help

Thanks.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜