Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

ruby on rails - NoMethodError in Person#new

undefined method `people_path' for #<#:0x007f4be4bfbaa8> Extracted source (around line #4):

<h1>Person#new</h1>
<p>Find me in app/views/person/new.html.erb</p> 

<% form_for (@people) do |f|%>
<%= f.label :first_name%>
<%= f.text_field :first_name%>
<%= f.label :last_name%>
<%= f.text_field :last_name%>
<%= f.label :company%>
<%= f.text_field :company%>
<%= f.label :email%>
<%= f.text_field :email%>
<%= f.label :phone%>
<%= f.text_field :phone%>
<%end%>

---- Person Controller ----

  def new
    @people = Person.new
  end

I have an error when I call person/new

----routes-----
get "person/new"
get "person/index"
get "person/show"
get "person/delete"
get "person/update"
get "person/create"

resources :person

----migrate----
class CreatePeople < ActiveRecord::Migration
def change
create_table :people do |t|
t.string :first_name
t.string :last_name
t.string :company
t.string :email
t.string :phone
t.timestamps
end
end
end

--model--
class Person < ActiveRecord::Base
end

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In routes.rb, your routes should be defined as below:

resources :people

This way people_path would be available. After this, you can run rake routes and check the routes created for you as below:

people      GET    /people(.:format)                     people#index
            POST   /people(.:format)                            people#create
new_person  GET    /people/new(.:format)             people#new
edit_person GET    /people/:id/edit(.:format)       people#edit
person      GET    /people/:id(.:format)                 people#show
            PATCH  /people/:id(.:format)                        people#update
            PUT    /people/:id(.:format)                        people#update
            DELETE /people/:id(.:format)                        people#destroy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...