我按照devise wiki來啓用用戶名登錄與設計(而不是電子郵件)。但我無法弄清楚如何啓用用戶名註冊/設計註冊。使用用戶名創建一個帳戶
我已更改我的註冊視圖以提示輸入姓氏和密碼。
應用程序/視圖/設計/註冊/ new.html.erb:
<h3>Sign up</h3>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :firstname, "First Name", :autofocus => true %><br />
<script type="text/javascript">window.onload = function() {document.getElementById('user_firstname').focus();}</script>
<%= f.text_field :firstname %></div>
<div><%= f.label :lastname, "Last Name", :autofocus => true %><br />
<%= f.text_field :lastname %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
我已經編輯我的模型來生成從姓和名用戶名:
應用程序/模型/ user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:username]
# Setup accessible (or protected) attributes for your model
#attr_accessor :username
validates_presence_of :firstname, :lastname, :unique => true
after_validation :createUsername
attr_accessible :username, :password, :password_confirmation, :remember_me, :firstname, :lastname
#attr_protected :password
def createUsername
# create username from first-last name
firstnamePart=self.firstname[0,1].downcase
lastnamePart=self.lastname[0,5].downcase
username=lastnamePart + firstnamePart
count=0
while username.length != 7
username=username + count.to_s
count +=1
end
self.username=username
#verify username uniqueness?
end
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
end
但是,當我嘗試註冊時出現以下錯誤。文本將不得不這樣做,因爲我沒有足夠的「聲譽」,發佈圖片:
Sign up
1 error prohibited this user from being saved:
Email can't be blank
因此,設計在註冊/登記階段仍然在尋找一個電子郵件。我想我必須編輯色器件的控制器,但沒有得到安裝在以下命令
- 軌摹色器件:安裝
- 軌摹色器件用戶
- 軌摹色器件:觀點
所以我的Rails應用程序可能使用了默認的設計控制器(/usr/local/rvm/gems/ruby-2.0.0-p195/gems/devise-1.5.4/app/controllers/devise/registrations_controller.rb),但我在那裏沒有看到任何對「電子郵件」的引用。
如何在註冊/註冊期間更改設計以接受用戶名?任何幫助將不勝感激。
我要檢查的第一件事就是確保'email'列從'users'表中消失。 –
好的,電子郵件字段仍在用戶表中,但我已將其刪除。 – olveram
所以,刪除用戶表中的電子郵件列後,我重新啓動我的應用程序,並嘗試註冊一個新的用戶名和姓氏後出現此錯誤:http://pastebin.com/PWfBNxu0 – olveram