我目前正在尝试使用devise on rails(5.2.2)和ruby版本2.3.7上的devise注册/创建应用程序帐户,但是我收到了不允许的参数消息并尝试使用Strong parms,但是它没有工作。
在下面尝试了这两个帖子,但是没有用
Rails 4 and Devise: Devise not saving new information (First Name, Last Name, Profile Name)
Rails 4 and Devise: Devise not saving new information (First Name, Last Name, Profile Name)
class Api::V3::ApplicationController < ActionController::Base
protect_from_forgery with: :null_session
before_action :configure_permitted_parameters, if: :devise_controller?
# before_filter :authenticate_user!
private
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:firstname,:lastname,:username,:password])
# devise_parameter_sanitizer.for(:sign_up) << :provider
# devise_parameter_sanitizer.for(:sign_up) << :uid
end
end
通过Devise :: RegistrationsController#create作为JSON处理
参数:{“ user” => {“ email” =>“ testuser@gmail.com”, “ password” =>“ [FILTERED]”,“ firstname” => devise“,” lastname“ =>” test“}} 不允许的参数::firstname,:lastname
看起来devise不接受参数,导致名字和姓氏未存储在本地数据库中。
答案 0 :(得分:0)
您可以覆盖注册控制器并添加所需的字段。
class RegistrationsController < Devise::RegistrationsController
def create
build_resource(sign_up_params)
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
respond_with resource, :location => after_sign_up_path_for(resource)
end
else
clean_up_passwords
respond_with resource
end
end
# you will get the full registration controller in devise github repo
private
def sign_up_params
params.require(:user).permit(:email, :first_name, :last_name, :password, :password_confirmation)
end
end
并添加新路线
devise_for :users, :controllers => { :registrations => 'registrations' }
设计github存储库here