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

标签 mysql ruby-on-rails activerecord

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

我创建了一个简单的学生 Controller 来为学生执行基本的创建、列表操作。

这是学生 Controller 中创建操作的列表:

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

student_params 是私有(private)方法:

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 %>

最佳答案

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

关于Mysql2::错误:字段 'teacherid' 没有默认值:INSERT INTO `students` VALUES (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23707231/

相关文章:

mysql - 如何在codeigniter中搜索文本?

mysql - 特定的MySQL批量插入性能调优

php - 无法将输入的 $username 插入表中

php - fatal error : Out of memory Zend Error/PhpMyAdmin

ruby-on-rails - example.com 重定向您的次数过多。 ERR_TOO_MANY_REDIRECTS

ruby-on-rails - ruby rails : question about validates_presence_of

基于 SELECT 的 MySQL DELETE

ruby-on-rails - Ruby Hash 和 ActiveSupport HashWithIndifferentAccess 的区别

css - Rails 4 - Heroku,预编译 Assets 失败

ruby-on-rails - Rails - 在 .where 查询中使用枚举