I'm getting started with Ruby on Rails and am following an online tutorial. The tutorial had me modify my routes.rb file by adding the following line:
resources :people
I also typed in rails generate controller People
previously. controllers/people_controllers.rb looks like this:
class PeopleController < ApplicationController
end
Everything looks good to me. However, http://0.0.0.0:3000/people
gives me an error:
Unknown action
The action 'index' could not be found for PeopleController
I don't think I should need to do this, but when I add:
def index
end
to my controller, and refresh the page, I get the following error instead:
Template is missing
Missi开发者_高级运维ng template people/index, application/index with {:handlers=>[:erb, :builder], :locale=>[:en, :en], :formats=>[:html]}. Searched in: * "/Users/myuser/projects/project_manager/app/views"
I'm using the newest version of Ruby, and Rails v3. Everything was installed today. What could be wrong here?
You've created the controller but not defined the view for it. Add a file called index.html.erb
in directory app/views/people
.
Alternatively you can re-run the controller generator to generate the index view:
rails g controller People index
Also it is a good idea to read the Rails Guides. For your understanding of views and rendering read this.
Just to help complete this thread, another potential cause of this error is when you forget that at some point during development you changed the signature of the associated controller method as though it would be used internally by adding, for example, a formal parameter list, e.g. you changed
def my_controller_method
to
def my_controller_method(my_parameter)
when you should instead have been using the former signature and catching the parameters from the view using, e.g.
params[:my_parameter]
The latter signature won't match up with the one the view is sending which will generate this error.
Yet another cause is when you forgot that you made the target method private. Move it to the public section of your controller to make it visible to the view (and to other controllers).
instead of:
resources :people
use
resources :peoples
Remember, the rails compiler always look for plurality of controller class
精彩评论