[Rails] 在devise註冊頁面中加入自訂欄位

Wendy Wu
Wendy Wu
Sep 8, 2018 · 3 min read

Devise是rails社群常用的使用者認證套件,還能產生html樣板,提供註冊、登入、修改密碼等頁面,註冊的頁面預設是這樣:

Default Sign up

那如果想要在註冊的時候,新增自定義的欄位,該怎麼做呢?

schema.rb
  • 至app/controllers/application_controller.rb加上permit additional parameters的設定(參考devise的說明
application_controller.rb
  • 至app/views/devise/registrations/new.html.erb,加上name的欄位
new.html.erb
  • 若要加入驗證name資料不能為空,可以參考validations的presense部分,在app/models/user.rb中加入
user.rb

這樣就完成在註冊頁面新增其他輸入欄位了!

Finish Sign up

其他參考資料:

https://ihower.tw/rails/auth.html

http://lichen-blog.logdown.com/posts/1830286

W-Learning Note

Learning Ruby on Rails

Wendy Wu

Written by

Wendy Wu

My Learning Note

W-Learning Note

Learning Ruby on Rails

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade