添加额外的注册字段与Devise [英] Adding extra registration fields with Devise
问题描述
我正在尝试添加一些额外的字段到注册#新的。因为我只想要额外的数据,而且不需要不同的功能,所以我不明白为什么我需要重写控制器等等。所以我做的是修改注册#新的如下:
I am trying to add some extra fields to registrations#new. Since I only want extra data and do not need different functionality, I don't see why I need to override controllers etc. So what I did was modify registrations#new as follows:
要启用这些额外的字段通过消毒剂,我更新了ApplicationController如下:
To enable these extra fields through the sanitizer, I updated ApplicationController as follows:
由于某种原因,它不工作,额外的字段作为空值进入数据库。
For some reason, it is not working and the extra fields go to the database as nulls.
我正在使用Ruby 2和Rails 4 rc1,与Devise 3.0.0.rc。
I am using Ruby 2 and Rails 4 rc1, with Devise 3.0.0.rc.
推荐答案
好的,所以我做的只是覆盖Devise注册控制器,根据设计文档更新routes.rb以反映这一点,复制并粘贴用于注册#创建的Devise代码,并更改获取参数部分以使用自己的强参数法,和那就是那个。
OK, so what I did was just override the Devise registration controller, update routes.rb as per the devise docs to reflect this, copied and pasted the Devise code for registrations#create as is, and change the getting params part to use my own strong parameters method, and that was that.
class RegistrationsController < Devise::RegistrationsController
def create
build_resource(registration_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
private
def registration_params
params.require(:user).permit(:email, :title_id, :first_name, :last_name,
:province_id, :password, :password_confirmation)
end
end
这篇关于添加额外的注册字段与Devise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!