저는 Rails 4.0과 Devise 3.0.0.rc.을 사용하고 있습니다.Devise with Rails를 사용하여 사용자 이름으로 로그인 4
기본 설정으로 Devise를 설치했습니다. 즉, 귀하의 이메일 주소를 사용하여 가입/사용하고 있습니다. 그러나 사용자가 이메일 주소 대신 사용자 이름으로 가입 할 수있게하고 싶습니다. railscasts에 따라 어떤 매개 변수를 변경해야하는지, 그리고 Rails 4에서 변경해야 할 사항에 대해 여기에서 찾을 수있는 조언이 있습니다. 그러나 새 사용자를 만들려고 할 때마다 오류가 발생합니다. 이메일은 비워 둘 수 없습니다 '. 누구나이 문제의 원인에 대해 아이디어를 가지고 있으며 어떻게 해결할 수 있습니까? 감사!!
코드 :
이응용 프로그램 컨트롤러 :에
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :password, :password_confirmation) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :password, :remember_me) }
end
end
로그인 (가입 형태는 거의 동일) :
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :username %><br />
<%= f.text_field :username, :autofocus => true %></div>
devise.rb 설정에서 :
config.authentication_keys = [ :username ]
사용자 cont 롤러 :
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:first_name, :middle_names, :last_name, :date_of_birth, :gender, :line_1, :line_2, :line_3, :town, :county, :postcode, :contact_number, :email, :username, :password, :password_confirmation, :remember_me)
end
user.rb :
protected
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value", { :value => login.downcase }]).first
end
정말 잘됐다. 고마워. 내가 그걸 놓쳤다는 것을 믿을 수 없어! 아마도 Devise는 전자 메일이 아닌 사용자 이름과 같은 공통 옵션으로 모든 유효성 검사를 변경하는 옵션을 추가해야합니다. – ecs
필요하지 않을 수도 있습니다. validatable 모듈을 사용하고'def email_required? '메소드를 정의함으로써 이메일 존재 여부 요구 사항을 비활성화 할 수 있습니다. false end'를 User 클래스에 추가합니다. – eugen
고마워, 나는 이것을 몇 시간 동안 고치려고 노력 해왔다. – Michael