Ruby-on-rails 添加自定义字段/列以使用 Rails 4 进行设计
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16297797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Custom Field/Column to Devise with Rails 4
提问by Neil Kelty
I'm attempting to add a full_namefield/column to my User model (using the devisegem) and Rails 4.
我正在尝试向full_name我的用户模型(使用devisegem)和 Rails 4添加一个字段/列。
Most of the examples online recommend using attr_accessible, but it sounds like this should be approached differently in Rails 4.
大多数在线示例都推荐使用 attr_accessible,但听起来在 Rails 4 中应该采用不同的方法。
How would I add full_nameto my User model? I've been able to successfully run the migration.
我将如何添加full_name到我的用户模型中?我已经能够成功运行迁移。
File: Migration > add_full_name_to_users
文件:迁移 > add_full_name_to_users
class AddFullNameToUsers < ActiveRecord::Migration
def change
add_column :users, :full_name, :string
end
end
File: Registration > app/views/devise/registration/new.html
文件:注册> app/views/devise/registration/new.html
.
.
.
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.label :full_name %>
<%= f.text_field :full_name, :autofocus => true %>
<%= f.label :email %>
<%= f.email_field :email %>
.
.
.
回答by Y3mSHF
Once your model has its full_name attribute, you will have to configure permitted parameters for the #sign_up and #account_update Devise actions.
一旦您的模型具有其 full_name 属性,您就必须为 #sign_up 和 #account_update Devise 操作配置允许的参数。
class ApplicationController < ActionController::Base
before_action :configure_devise_permitted_parameters, if: :devise_controller?
protected
def configure_devise_permitted_parameters
registration_params = [:full_name, :email, :password, :password_confirmation]
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
回答by yozzz
This solution should work, working with sign_upand update:
这个解决方案应该可以工作,使用sign_up和update:
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:full_name])
devise_parameter_sanitizer.permit(:account_update, keys: [:full_name])
end
end
回答by Leo
From devise documentation:
从设计文档:
When you customize your own views, you may end up adding new attributes to forms. Rails 4 moved the parameter sanitization from the model to the controller, causing Devise to handle this concern at the controller as well.
当您自定义自己的视图时,您最终可能会向表单添加新属性。Rails 4 将参数清理从模型转移到控制器,导致 Devise 在控制器上处理这个问题。
You should check the url below to find the approach that will best fit your needs: https://github.com/plataformatec/devise#strong-parameters
您应该检查下面的网址以找到最适合您需求的方法:https: //github.com/plataformatec/devise#strong-parameters
回答by mouhcine
Enable Strong Parameters for Devise instead of attr_accessible. To do so, create a new initiliazer with that content:
为设计启用强参数而不是 attr_accessible。为此,请使用该内容创建一个新的初始化程序:
DeviseController.class_eval do
def resource_params
unless params[resource_name].blank?
params.require(resource_name).permit(:email, :password, :password_confirmation, :remember_me)
end
end
end

