2013-09-29 151 views
1

我創建一個Rails的博客,我填充數據庫test samples,現在當我運行db:resetdb:migrate,在數據庫中創建表,但顯示了以下錯誤,同時創造了新的文章或作者。的ActiveRecord :: StatementInvalid在AuthorsController#創建

Mysql2::Error: Field 'username' doesn't have a default value: INSERT INTO `authors` (`created_at`, `updated_at`) VALUES ('2013-09-29 05:30:43', '2013-09-29 05:30:43') 

幫助表示讚賞。

提取的源

respond_to do |format| 
    **if @author.save** 
    format.html { redirect_to @author, notice: 'Author was successfully created.' } 
    format.json { render action: 'show', status: :created, location: @author } 
    else 

if @author.save

錯誤亮點Schema.rb

# encoding: UTF-8 
# This file is auto-generated from the current state of the database. Instead 
# of editing this file, please use the migrations feature of Active Record to 
# incrementally modify your database, and then regenerate this schema definition. 
# 
# Note that this schema.rb definition is the authoritative source for your 
# database schema. If you need to create the application database on another 
# system, you should be using db:schema:load, not running all the migrations 
# from scratch. The latter is a flawed and unsustainable approach (the more migrations 
# you'll amass, the slower it'll run and the greater likelihood for issues). 
# 
# It's strongly recommended that you check this file into your version control system. 

ActiveRecord::Schema.define(version: 20130928053429) do 

    create_table "articles", force: true do |t| 
    t.string "title" 
    t.text  "body" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "image_file_name" 
    t.string "image_content_type" 
    t.integer "image_file_size" 
    t.datetime "image_updated_at" 
    end 

    create_table "authors", force: true do |t| 
    t.string "username",   null: false 
    t.string "email" 
    t.string "crypted_password" 
    t.string "salt" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "comments", force: true do |t| 
    t.string "author_name" 
    t.text  "body" 
    t.integer "article_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "comments", ["article_id"], name: "index_comments_on_article_id", using: :btree 

    create_table "pages", force: true do |t| 
    t.string "title" 
    t.text  "body" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "taggings", force: true do |t| 
    t.integer "tag_id" 
    t.integer "article_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "taggings", ["article_id"], name: "index_taggings_on_article_id", using: :btree 
    add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id", using: :btree 

    create_table "tags", force: true do |t| 
    t.string "name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

end 

作者控制器

class AuthorsController < ApplicationController 
    before_action :set_author, only: [:show, :edit, :update, :destroy] 


    def zero_authors_or_authenticated 
    unless Author.count == 0 || current_user 
     redirect_to root_path 
     false 
    end 
    end 

    # GET /authors 
    # GET /authors.json 
    def index 
    @authors = Author.all 
    end 

    # GET /authors/1 
    # GET /authors/1.json 
    def show 
    end 

    # GET /authors/new 
    def new 
    @author = Author.new 
    end 

    # GET /authors/1/edit 
    def edit 
    end 

    # POST /authors 
    # POST /authors.json 
    def create 
    @author = Author.new(author_params) 

    respond_to do |format| 
     if @author.save 
     format.html { redirect_to @author, notice: 'Author was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @author } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @author.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /authors/1 
    # PATCH/PUT /authors/1.json 
    def update 
    respond_to do |format| 
     if @author.update(author_params) 
     format.html { redirect_to @author, notice: 'Author was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @author.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /authors/1 
    # DELETE /authors/1.json 
    def destroy 
    @author.destroy 
    respond_to do |format| 
     format.html { redirect_to authors_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_author 
     @author = Author.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def author_params 
     params.require(:author).permit(:username, :email, :password, :password_confirmation) 
    end 
end 

當試圖創建一個新的autho r使用Rails控制檯,彈出以下錯誤消息。

Author.new(username: 'ajay', email: '[email protected]', password: '12345', password_confirmation: '12345').save 
WARNING: Can't mass-assign protected attributes for Author: username, email, password, password_confirmation 

    (0.3ms) BEGIN 
ActiveRecord::StatementInvalid: Mysql2::Error: Field 'username' doesn't have a default value: INSERT INTO `authors` (`created_at`, `updated_at`) VALUES ('2013-09-29 06:00:23', '2013-09-29 06:00:23') 
    from /home/aj/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:286:in `query' 
    SQL (0.7ms) INSERT INTO `authors` (`created_at`, `updated_at`) VALUES ('2013-09-29 06:00:23', '2013-09-29 06:00:23') 
Mysql2::Error: Field 'username' doesn't have a default value: INSERT INTO `authors` (`created_at`, `updated_at`) VALUES ('2013-09-29 06:00:23', '2013-09-29 06:00:23') 
    (0.2ms) ROLLBACK 
+0

什麼您的架構/遷移是什麼樣子? – seand

+0

問題已更新。 – ajkumar25

回答

3

在你schema.rb它說,一個username不允許爲空:

t.string "username",   null: false 

你可以看到用戶名沒有被設置的錯誤信息,所以數據庫被拒絕新行。

INSERT INTO `authors` (`created_at`, `updated_at`) VALUES ('2013-09-29 05:30:43', '2013-09-29 05:30:43') 

您應該確保您設置的是username

[更新]以響應原來的問題的更新:

現在,你應該尋找在關鍵的就是這一行:

WARNING: Can't mass-assign protected attributes for Author: username, email, password, password_confirmation 

你想閱讀了關於attr_accessibleattr_protected。您可能需要在您的Author模型中使用類似的東西。

attr_accessible :username, :email, :password, :password_confirmation 
+0

查看更新的問題。 – ajkumar25

+0

添加了[更新]以迴應您答案中的新信息。 –

0

當試圖創建一個新的'authors'行時,顯然缺少架構配置爲'not null'的'username'字段。

有些時候應該設置@ authors.username。這可以通過顯式調用 @ authors.username = params [...或者一個質量分配。

0

看起來你需要設置你的模型你attr_accessible聲明:

class Author < ApplicationController 
    attr_accessible :username, :email # etc 
end 
相關問題