Mysql2 ::错误:字段' teacherid'没有默认值:INSERT INTO`school` VALUES()

时间:2014-05-17 03:19:55

标签: mysql ruby-on-rails activerecord

我正在编写一个围绕学生和教师的Ruby on Rails应用程序

我创建了一个简单的学生控制器来为学生做基本的创建,列出动作。

这是学生控制器中创建操作的列表:

def create
  @student = Student.new(student_params)
  if @student.save
    redirect_to :action => 'index'
  else
    render :action => 'new'
  end
end

student_params是一种私人方法:

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

这就是我在学生模型中所拥有的:

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

这是我定义表格的方式:

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

当我为新学生保存表单时,它又在form_tag中调用create方法,我在浏览器中收到此错误:

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'

以下是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 %>

1 个答案:

答案 0 :(得分:-1)

尝试从模型中删除attr_accessor行。您不需要它,它可能会覆盖ActiveRecord :: Base中的默认设置器逻辑。