Reputation: 1805
i am getting this error for a while my student_controller.rb file:
class StudentController < ApplicationController
def new
@student=Student.new
end
def create
@student=Student.new(params[:student])
if @student.save
redirect_to new_student_path
end
end
end
and my new.html.erb file:
student infos
<%= form_for Student.new do |f| %>
firstname:<%= f.text_field :firstname %> <br/>
lastname:<%= f.text_field :lastname %> <br>
<%= f.submit %>
<% end %>
the student_controller.rb file is located in controller folder and new.html.erb file is located in /views/student/ my ruby version is 2.3.0p0 and my rails version is 5.0.0 please any help and my routes.rb file:
Rails.application.routes.draw do
resources :student
end
Upvotes: 1
Views: 3292
Reputation: 3685
try to rename your controller in students_controller.rb
. Note students is plural.
Also rename class name from:
class StudentController < ApplicationController
to:
class StudentsController < ApplicationController
EDIT
You can write your create action as follow:
def create
@student = Student.new(student_params)
if @student.save
redirect_to new_student_path
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def student_params
params.require(:student).permit(:firstname, :lastname)
end
Upvotes: 2
Reputation: 3427
<%= form_for Student.new do |f| %>
will submit to StudentsController#create
action. Form action will be "/students" and corresponding named route will be students_path, But you have defined singular routes for student
So either
Rename StudentController
to StudentsController
and make resources route plural as resources :students
or
Run $ rake routes
, find student create path & Override automatic form action generation by giving url parameter
e.g
<%= form_for Student.new, url: student_index_path do |f| %>
Upvotes: 0