I am writing a Ruby on Rails application revolving around students and teachers
I created a simple Student controller to do basic Create, list actions for student.
This is the listing of the create action in the Student controller:
def create
@student = Student.new(student_params)
if @student.save
redirect_to :action => 'index'
else
render :action => 'new'
end
end
student_params is a private method:
private
def student_params
params.require(:student).permit(:teacherid, :firstname, :lastname, :dob, :email,
:cellphone, :username, :password,
:addr_streetno,
:addr_aptno, :addr_city, :addr_state,
:addr_zip, :photo)
end
end
This is what I have in the student model:
class Student < ActiveRecord::Base
attr_accessor :teacherid, :firstname, :lastname, :dob, :isadult,
:email, :cellphone, :username, :password, :addr_streetno,
:addr_city, :addr_state, :addr_zip, :photo
scope :sorted, lambda { order("students.firstname ASC")}
end
This is how I have defined the table:
create_table "students", primary_key: "studentid", force: true do |t|
t.integer "teacherid", null: false
t.integer "parentid"
t.string "firstname", limit: 45, null: false
t.string "lastname", limit: 45, null: false
t.datetime "dob", null: false
t.boolean "isadult", null: false
t.string "email", limit: 45, null: false
t.integer "cellphone"
t.string "username", limit: 45, null: false
t.string "password", limit: 45, null: false
t.string "addr_streetno", limit: 45
t.integer "addr_aptno"
t.string "addr_city", limit: 45
t.string "addr_state", limit: 45
t.integer "addr_zip"
t.binary "photo", limit: 16777215
end
When I am saving the form for a new student, which is in turn calling the create method in the form_tag, i am getting this error in the browser:
ActiveRecord::StatementInvalid in StudentController#create
Mysql2::Error: Field 'teacherid' doesn't have a default value: INSERT INTO `students` VALUES ()
Extracted source (around line #16):
14 def create
15 @student = Student.new(student_params)
16 if @student.save
17 redirect_to :action => 'index'
18 else
19 render :action => 'new'
Here is the code for the new.html.erb:
<h1>Add A Student</h1>
<%= form_tag(:action=> 'create', :multipart =>true) do %>
<table summary="Student form fields">
<tr>
<th>Teacher ID</th>
<td><%= number_field(:student, :teacherid) %></td>
</tr>
<tr>
<th>FirstName</th>
<td><%= text_field(:student, :firstname) %></td>
</tr>
<tr>
<th>LastName</th>
<td><%= text_field(:student, :lastname) %></td>
</tr>
<tr>
<th>Date of Birth</th>
<td><%= date_field(:student, :dob) %></td>
</tr>
<tr>
<th>Email</th>
<td><%= email_field(:student, :email) %></td>
</tr>
<tr>
<th>Cellphone</th>
<td><%= telephone_field(:student, :cellphone) %></td>
</tr>
<tr>
<th>Username</th>
<td><%= text_field(:student, :username) %></td>
</tr>
<tr>
<tr>
<th>Password</th>
<td><%= password_field(:student, :password, size: 20) %></td>
</tr>
<tr>
<th>Address Street#</th>
<td><%= text_field(:student, :addr_streetno) %></td>
<th>Apt #</th>
<td><%= number_field(:student, :addr_aptno) %></td>
</tr>
<tr>
<th>City</th>
<td><%= text_field(:student, :addr_city) %></td>
<th>State</th>
<td><%= text_field(:student, :addr_state) %></td>
<th>Zip</th>
<td><%= number_field(:student, :addr_zip) %></td>
</tr>
<tr>
<th>Photo</th>
<td><%= file_field(:student, :photo) %></td>
</tr>
</table>
<%= submit_tag('Create Student') %>
<% end %>