chenio
chenio

Reputation: 592

Routing Error Ruby on Rails

I've installed ruby on rails 3.2.6, and when I execute rails server

and access to 127.0.0.1:3000 it works, however when I generate a controller, for example

rails generate controller principal

and access to 127.0.0.1:3000/somecontroller, browser show following error: Routing Error

No route matches [GET] "/principal" Try running rake routes for more information on available routes.

What do I need to do, and can this be simply explained?

Upvotes: 0

Views: 147

Answers (2)

Jim Riordan
Jim Riordan

Reputation: 1428

You need to edit config/routes.rb to tell the router which controller to route your request to. The rails standard is to use RESTful routes and in the example you've given this would equate to a singular Principal resource. Therefore you'd need to add:

resource :principal

to generate a set of RESTful routes for this resource. You can see the routes generated by doing:

rake routes

If you don't care about REST then you can simply add (assuming the PrincipalController has an index method):

match 'principal' => 'principal_controller#index'

Have a look at this chapter from the Rails Guides for more info on routing:

http://guides.rubyonrails.org/routing.html

You can also generate methods in the controller and routes at the same time by supplying their names as arguments to the rails generate controller command for example:

rails generate controller principal index

Upvotes: 1

Carson Cole
Carson Cole

Reputation: 4461

The problem is you did not specify any actions, so your controller 'principal' is empty, no views will be created with similar names, and no routes created.

You need to do:

rails generate controller principal index [show] [edit] [update] [create] [destroy]

The name after your controller name are the action names. Since you said controller 'principal' in the singular, then it might imply that you have a singular resource. If you want to have it in the plural, make sure you say 'controller principals'.

And your routes should should show:

resource :principal [ or :principals or multiple Restful routes ]

Upvotes: 2

Related Questions