Lee*_*Lee 18 ruby-on-rails devise
我正在尝试为注册#new添加一些额外的字段.由于我只需要额外的数据并且不需要不同的功能,我不明白为什么我需要覆盖控制器等.所以我做的是修改注册#new如下:

要通过清理程序启用这些额外字段,我更新了ApplicationController,如下所示:

由于某种原因,它不起作用,额外的字段作为空值进入数据库.
我正在使用Ruby 2和Rails 4 rc1,以及Devise 3.0.0.rc.
Dan*_*tes 30
您的问题中的代码示例似乎无效,因为您没有设置before_filter来调用清理程序.
before_filter :configure_permitted_parameters, if: :devise_controller?
Run Code Online (Sandbox Code Playgroud)
如上所述,覆盖控制器可能更好,如接受的答案所示,这样应用程序控制器就不会一直进行此检查.接受的答案可以通过以下代码缩短.我用我的应用程序测试了这段代码,效果很好.所有这些都记录在3.0.0.rc标记的README的Strong Parameters部分中.
覆盖控制器:
class RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters, :only => [:create]
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password) }
end
end
Run Code Online (Sandbox Code Playgroud)
然后更新路由以使用它:
devise_for :members, :controllers => { :registrations => "registrations" }
Run Code Online (Sandbox Code Playgroud)
Lee*_*Lee 10
好的,所以我做的只是覆盖Devise注册控制器,根据设计文档更新routes.rb以反映这一点,复制并粘贴注册的Devise代码#create as is,并更改获取params部分以使用我自己的强参数法,就是这样.
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
Run Code Online (Sandbox Code Playgroud)
在Devise 4.0之后,关于此主题的较旧答案无效.而不是for你必须使用的方法:
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
Run Code Online (Sandbox Code Playgroud)
因此,要获得完整的解决方案ApplicationController:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end
Run Code Online (Sandbox Code Playgroud)
小智 9
自2017年5月15日的Devise版本4.3.0起,解决方案如下所示.在这种情况下,正在添加用户名字段.
如果您想要允许其他参数(lazy way™),您可以使用ApplicationController中的简单前置过滤器:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end
Run Code Online (Sandbox Code Playgroud)
当然,只需将字段添加到数据库即可
> rails g migration AddUsernameToUsers
class AddUsernameToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :username, :string, null: false, index: true, unique: true
end
end
Run Code Online (Sandbox Code Playgroud)
然后将必要的字段添加到视图中以进行注册#new
<%= f.text_field :username, placeholder: "Username" %>
Run Code Online (Sandbox Code Playgroud)
首先公开视图
rails generate devise:views users
Run Code Online (Sandbox Code Playgroud)
然后编辑config/initializers/devise.rb并进行更改
# config.scoped_views = false
Run Code Online (Sandbox Code Playgroud)
至
config.scoped_views = true
Run Code Online (Sandbox Code Playgroud)
这将允许您修改app/views/users/registration中的视图.
你将在这两个地方添加这里需要的字段
app/views/users/registration/edit.html.erb
app/views/users/registration/new.html.erb
Run Code Online (Sandbox Code Playgroud)
现在我们必须处理rails mass assignment问题,转到application_controller.rb并添加一个before_filter
before_filter :configure_permitted_parameters, if: :devise_controller?
Run Code Online (Sandbox Code Playgroud)
然后添加你的字段+原始字段来设计清理
protected
def configure_permitted_parameters
# Fields for sign up
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password) }
# Fields for editing an existing account
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :current_password, :gender) }
end
Run Code Online (Sandbox Code Playgroud)
重新启动您的Web服务器并交叉手指.
小智 6
我有类似的情况(只是字段不同).
这是官方文档提供的方式:只需将其添加到ApplicationController即可.并将"用户名"更改为您需要的任何内容,并根据需要添加更多内容.
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
end
Run Code Online (Sandbox Code Playgroud)
我的应用控制器看起来像这样:
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_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :public_name
end
end
Run Code Online (Sandbox Code Playgroud)
更多细节:https://github.com/plataformatec/devise("强参数")