Reputation: 100
I'm learning rails and have encountered a problem I can not find a solution to.
When I am using a form for editing and creating new persons, I get this error
undefined method 'people_index_path' for #<#<Class:0x007f68442da790>:0x007f6850013410>
On the following line.
<%= form_for @people do |f| %>
This is what my rake routes look like.
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
events GET /events(.:format) events#index
POST /events(.:format) events#create
new_event GET /events/new(.:format) events#new
edit_event GET /events/:id/edit(.:format) events#edit
event GET /events/:id(.:format) events#show
PATCH /events/:id(.:format) events#update
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
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
And the controller
class PeopleController < ApplicationController
def new
@people = People.new
end
def edit
@people = People.find(params[:id])
end
def update
@people = People.find(params[:id])
if @people.update(people_params)
redirect_to people_path
else
render 'edit'
end
end
def show
@people = People.find(params[:id])
end
def index
@all_people = People.all
end
def create
@people = People.new(people_params)
if @people.save
redirect_to @people
else
render 'new'
end
end
def destroy
@people = People.find(params[:id])
@people.destroy
redirect_to people_path
end
private
def people_params
params.require(:people).permit(:name, :pno, :gender)
end
end
schema.rb
ActiveRecord::Schema.define(version: 20140417200735) do
create_table "events", force: true do |t|
t.string "title"
t.string "location"
t.datetime "date"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "people", force: true do |t|
t.string "name"
t.string "pno"
t.string "gender"
t.datetime "created_at"
t.datetime "updated_at"
end
end
I would appreciate any help to solve this problem.
routes.rb
Nightlife::Application.routes.draw do
root 'welcome#index'
resources :events, :people
end
Full error message. I do only get errors while creating a new person.
NoMethodError in People#new
Showing /home/oalsing/Documents/Websites/Rails/Nightlife/app/views/people/_form.html.erb where line #1 raised:
undefined method `people_index_path' for #<#<Class:0x007f68442da790>:0x007f68481c42a8>
Extracted source (around line #1):
1
2
3
4
<%= form_for @people do |f| %>
<% if @people.errors.any? %>
<div id="error_explanation">
Trace of template inclusion: app/views/people/new.html.erb
Rails.root: /home/oalsing/Documents/Websites/Rails/Nightlife
Application Trace | Framework Trace | Full Trace
app/views/people/_form.html.erb:1:in `_app_views_people__form_html_erb__3105468428477628872_70042931049120'
app/views/people/new.html.erb:3:in `_app_views_people_new_html_erb__2301167718720189466_70042999118360'
Request
Parameters:
None
Upvotes: 0
Views: 351
Reputation: 24815
I wonder why everybody here don't point out the problem in model name.
If you have a PeopleController
, the model should be Person
but NOT People
because "people" is plural of "person".
And the instance name should be @person
but not @people
.
Rails convention is not a must. But please respect it when you can. Only break it when you really have to and know every in and out.
The named path has already bite you. You'll see more for breaking the convention.
Upvotes: 1
Reputation: 17834
Try this when the form is same
<%= form_for @people, :url => (@people.new_record? ? (people_path) : (person_path(@people))), :method => (@people.new_record? ? "post" : "put") do |f| %>
Upvotes: 1
Reputation: 11116
thats a long standing bug , Check rails issues page
This works in case of edit but wont work on create.
This is what the guides have to say:
A long-standing bug prevents form_for from working automatically with singular resources. As a workaround, specify the URL for the form directly, like so:
form_for @geocoder, url: geocoder_path do |f|
so in your case ,
<%= form_for @people, url: people_path do |f| %>
should work.
Upvotes: 1
Reputation: 33542
As per the comments,you are trying to create a new People
record,so change your code to like this
<%= form_for @people, url: {action: "create"} do |f| %>
Upvotes: 1