我有以下ApplicationController:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
before_filter :require_vote
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :email])
# devise_parameter_sanitizer.for(:sign_up) << :username
end
private
def require_vote
unless cookies[:voted]
redirect_to vote_path
end
end
end
我也有以下用户模型:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :pictures
attr_accessor :username
validates :username, presence: true
end
当我注册时,在日志中我看到它实际上并没有在数据库中插入用户名,但它在参数中获取用户名。我做错了什么?
答案 0 :(得分:0)
class ApplicationController < ActionController::Base
include CanCan::ControllerAdditions
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_devise_permitted_parameters, if: :devise_controller?
protected
def configure_devise_permitted_parameters
registration_params = [:name, :email, :phone, :skype, :site, :description, :image, :image_cache, :password, :password_confirmation, :surname, :patronymic]
if params[:action] == 'update'
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit(registration_params << :current_password)
end
elsif params[:action] == 'create'
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(registration_params)
end
end
end
end
试一试。