Rails 4.0 と Devise 3.0.0.rc を使用しています。
私は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 ]
ユーザーコントローラー:
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
ユーザー.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