1

我想要製作一個有兩種用戶(玩家和管理員)的Rails應用,但只會允許玩家註冊。嘗試插入數據庫時​​,我無法使註冊工作並收到「類型不能爲空」的錯誤。添加自定義類型到Rails用戶註冊

UsersController < ApplicationController 

def new 
    @player = Player.new 
    @player.type = 'Player' 
end 

def create 
    @player = Player.new(params[:player]) 
    @player.type = 'Player' 
    if @player.save 
    redirect_to @player 
    else 
    render 'new' 
end 

我的/註冊讓用戶提交電子郵件和密碼。在SQL插入工作在管理面板,看起來像

SQL (0.3ms) INSERT INTO "users" ("type", "email", "city", "display_name", "tags", "game_id", "affiliation", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["type", "Player"], ["email", "d"], ["city", ""], ["display_name", "d"], ["tags", ""], ["game_id", 1], ["affiliation", ""], ["encrypted_password", "$2a$10$AfuUFDjJRnoAl2fL3MGfNO9AEve.YNGNSp2SUhpfOiV6KuXuve2qC"], ["created_at", "2015-12-21 02:04:33.485089"], ["updated_at", "2015-12-21 02:04:33.485089"]] 

所以我試圖讓剛剛播放器類型的插入。

謝謝!

回答

0
"Type can't be blank" 

你是因爲你有你的Player模型驗證是type不能爲空,可能得到這個錯誤。

要解決該問題,如果您計劃在控制器中手動設置類型,或者添加類型字段並將其類型與播放器表單中的播放器對象一起提交,則可以移除驗證。

+0

偉大的工作!但是,我的語法是否適合添加到類型字段?我的SQL現在是 SQL(0.3ms)INSERT INTO「users」(「email」,「encrypted_pa​​ssword」,「created_at」,「updated_at」)VALUES(?,?,?,?)[[「email」, 「cccccc」],[「encrypted_pa​​ssword」,「$ 2a $ 10 $ 49GeV1gRDExnrbpbG25CIOdgbHtMrrdtRYYzKRXtEW33y06VWftCa」],[「created_at」,「2015-12-21 03:12:17.655950」],[「updated_at」,「2015-12-21 03 :12:17.655950「]] – 0xc0ff33

+0

看起來你不再保存'type'了。你能展示你的模型和控制器的最新代碼嗎? –

+0

是的,沒關係。 –

相關問題