0

デバイスを使用する登録ページに 1 つのフィールドを追加しようとしています。Deviseドキュメントの指示に従いました。新しいフィールドは「役割」フィールドです。私はすでにそれをデータベースに正常に移行しました。私は今、サインアップが機能するようにしようとしています。新しいアプリケーション コントローラは次のとおりです。

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

    def devise_parameter_sanitizer
        if resource_class == User
          User::ParameterSanitizer.new(User, :user, params)
        else
          super # Use the default one
        end
    end
end

そして、変更された User モデルは次のとおりです。

class User::ParameterSanitizer < Devise::ParameterSanitizer
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  def sign_up
    default_params.permit(:email, :role)
  end
end 

これら 2 つのファイルを追加すると、サーバーはロードできなくなります。役割フィールドを正常に追加するにはどうすればよいですか?

編集

ここに私のroutes.rbファイルがあります

devise_for :users
root 'static_pages#home'
get 'student' => 'static_pages#index'
4

1 に答える 1

6

次のアプローチを使用して、デバイス ユーザー テーブルにカスタム フィールドを追加することもできます。

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.

  before_filter :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
   devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:firstname, :lastname, :email, :password, :password_confirmation) } # The :firstname and :lastname are my custom fields.
  end

  protect_from_forgery with: :exception
end 
于 2013-10-15T08:17:39.970 に答える