Rails错误与Mysql2字段名称没有值

时间:2014-10-01 10:57:25

标签: ruby-on-rails

Mysql2::Error: Field 'USER_NAME' doesn't have a default value: INSERT INTO `dashboard_user` VALUES ()

当我尝试打开我的页面http//127.0.0.1:3000/my/user_creation

时,我收到此错误消息

有人可以帮忙吗?

下面是模型

class CreateDashboardUsers < ActiveRecord::Migration
  def change
    create_table "dashboard_user", primary_key: "USER_ID", force: true do |t|
    t.string   "USER_NAME",            limit: 50,  null: false
    t.string   "NORMALIZED_USER_NAME", limit: 50
    t.string   "PASSWORD",             limit: 50
    t.string   "LAST_NAME",            limit: 50
    t.string   "FIRST_NAME",           limit: 50
    t.string   "MIDDLE_NAME",          limit: 50
    t.string   "PHONE",                limit: 15
    t.string   "EMAIL_ID",             limit: 100
    t.integer  "SEQ_QUES_ID"
    t.string   "SEQ_QUES_ANSWER",      limit: 100
    t.string   "EXPIRE_PASSWORD_IND",  limit: 1
    t.date     "EXPIRE_PASSWORD_DATE"
    t.string   "DEACTIVATED_IND",      limit: 1
    t.date     "DEACTIVATED_DATE"
    t.integer  "ROLE_ID"
    t.string   "CREATED_BY",           limit: 50
    t.datetime "CREATED_DATE"
    t.string   "UPDATED_BY",           limit: 50
    t.datetime "UPDATED_DATE"
    end
  end
end

控制器代码在

之下
 class AdminController < ApplicationController
      def user_creation
         DashboardUser.create(:USER_NAME => params[:username])
         DashboardUser.create(:PASSWORD => params[:password])
         DashboardUser.create(:LAST_NAME => params[:lastname])
         DashboardUser.create(:FIRST_NAME => params[:firstname])
         DashboardUser.create(:MIDDLE_NAME => params[:middlename])
         DashboardUser.create(:PHONE => params[:phone])
        redirect_to :action => 'user_creation'
      end
      private
      def dashboard_params
      params.require(:dashboard_user).permit(:id, :user_name, :password, :last_name, :first_name, :middle_name , :phone)
      end

end

Mysql2 ::错误:字段'USER_NAME'没有默认值:INSERT INTO dashboard_user VALUES()这是我在浏览器中运行时获得的

dashboard_user

class DashboardUser < ActiveRecord::Base
  self.table_name = "dashboard_user"
end

1 个答案:

答案 0 :(得分:1)

这不是您创建ActiveRecord :: Base对象的方式。有关信息,请参阅http://guides.rubyonrails.org/active_record_basics.html#create

看看这是否有效。

DashboardUser.create(
  :USER_NAME => params[:username],
  :PASSWORD => params[:password],
  :LAST_NAME => params[:lastname],
  :FIRST_NAME => params[:firstname],
  :MIDDLE_NAME => params[:middlename],
  :PHONE => params[:phone]
)

(P.S在你做其他任何事情之前阅读基础知识会更好!)