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"]]
所以我試圖讓剛剛播放器類型的插入。
謝謝!
偉大的工作!但是,我的語法是否適合添加到類型字段?我的SQL現在是 SQL(0.3ms)INSERT INTO「users」(「email」,「encrypted_password」,「created_at」,「updated_at」)VALUES(?,?,?,?)[[「email」, 「cccccc」],[「encrypted_password」,「$ 2a $ 10 $ 49GeV1gRDExnrbpbG25CIOdgbHtMrrdtRYYzKRXtEW33y06VWftCa」],[「created_at」,「2015-12-21 03:12:17.655950」],[「updated_at」,「2015-12-21 03 :12:17.655950「]] – 0xc0ff33
看起來你不再保存'type'了。你能展示你的模型和控制器的最新代碼嗎? –
是的,沒關係。 –