为Spree Devise注册添加名称 [英] Adding name to Spree Devise registration

查看:163
本文介绍了为Spree Devise注册添加名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经安装了宝石

  gem'spree_auth_devise',github:'spree / spree_auth_devise',branch:'3-0-stable'

创建迁移:

  class AddFieldsToSpreeUsers< ActiveRecord :: Migration 
def change
add_column:spree_users,:name,:string
add_column:spree_users,:surname,:string
add_column:spree_users,,birthdate,,time
end
end

创建一个新表单来添加字段应用程序/views/spree/shared/_user_form.html.erb



在应用程序控制器中定义了一个运行前的操作的方法

  class ApplicationController< ActionController :: Base 
#通过引发异常来防止CSRF攻击。
#对于API,您可能需要使用:null_session。
protect_from_forgery with::exception

before_action:set_locale
before_filter:configure_permitted_pa​​rameters,if::devise_controller?

def set_locale
I18n.locale = params [:locale] || I18n.default_locale
end

protected

def configure_permitted_pa​​rameters
devise_parameter_sanitizer.for(:sign_up){| u | u.permit(:email,:password,:name,:surname,:birthdate)}
devise_parameter_sanitizer.for(:account_update){| u | u.permit(:email,:password,:name,,surname,:birthdate)}
end

end

我还没有在数据库中没有记录

 < Spree :: user id:2,encrypted_pa​​ssword:bdd86072513f789da5a395080e3d16e28c96cfe5e3aaea105b ...,
...,name:nil,surname:nil,birthdate:nil>

这是添加用户表单提交操作的控制台日志:

 开始POST/注册为:: 1在2015-07-21 09:52:08 -0500 
处理Spree :: UserRegistrationsController#创建为HTML
参数:{utf8=>✓,authenticity_token=>+ 9EoKHj9fkHqKF8TRtlcfIYt5 + QPuPa1ynmWVifUNY3luiCDpiBP9z2VV / uMAH1JP0CCg7gwG2gu7vO1TaSacw ==,spree_user=> {name=> Christophe,surname=>Mysurname,email=>email@gmail.com,password=>[FILTERED],birthdate=>1978-11 -11},commit=>创建}
未经允许的参数:姓名,姓氏,生日
(0.6ms)BEGIN

我做错了什么?我有未经允许的参数。

解决方案

这是基于以下帖子的答案:



Rails 4 - 强参数概念参与in spree-2.1



我已经设法添加名称,姓氏和birthdate,将下面的行添加到 config / initializers / spree.rb

  Spree :: PermittedAttributes.user_attributes.push:name,:surname,:birthdate 

我还将birthdate从时间类型更改为日期类型,并进行以下迁移:

  class ChangeBirthdateFromTimeToDate< ActiveRecord :: Migration 

def up
remove_column:spree_users,:birthdate,:time
add_column:spree_users,:birthdate,:date
end

def down
remove_column:spree_users,:birthdate,:date
add_column:spree_users,:birthdate,:time
end
end


I am trying to add name, surname and birthdate to devise registration with Spree.

I have installed the gem

gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '3-0-stable'

Created the migration:

    class AddFieldsToSpreeUsers < ActiveRecord::Migration
      def change
        add_column :spree_users, :name, :string
        add_column :spree_users, :surname, :string
        add_column :spree_users, :birthdate, :time
      end
    end

Created a new form to add the fields app/views/spree/shared/_user_form.html.erb

Defined a method in application controller to run with a before action

   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 :set_locale
      before_filter :configure_permitted_parameters, if: :devise_controller?

      def set_locale
        I18n.locale = params[:locale] || I18n.default_locale
      end

    protected

      def configure_permitted_parameters
        devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :name, :surname, :birthdate) }
        devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :name, :surname, :birthdate) }
      end

   end

And I am still getting no records in the database

<Spree::User id: 2, encrypted_password:   "bdd86072513f789da5a395080e3d16e28c96cfe5e3aaea105b...",
... , name: nil,  surname: nil, birthdate: nil> 

Here is the console log for the add user form submit action:

Started POST "/signup" for ::1 at 2015-07-21 09:52:08 -0500
Processing by Spree::UserRegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+9EoKHj9fkHqKF8TRtlcfIYt5+QPuPa1ynmWVifUNY3luiCDpiBP9z2VV/uMAH1JP0CCg7gwG2gu7vO1TaSacw==", "spree_user"=>{"name"=>"Christophe", "surname"=>"Mysurname", "email"=>"email@gmail.com", "password"=>"[FILTERED]", "birthdate"=>"1978-11-11"}, "commit"=>"Create"}
 Unpermitted parameters: name, surname, birthdate
 (0.6ms)  BEGIN

What am i doing wrong? I have unpermitted parameters.

解决方案

Here is the answer based on the following post:

Rails 4 - strong parameters concept involvement in spree-2.1

I have managed to add name, surname and birthdate by adding the line below to config/initializers/spree.rb

Spree::PermittedAttributes.user_attributes.push :name, :surname, :birthdate

I have also changed birthdate from a time type to a date type with the following migration:

class ChangeBirthdateFromTimeToDate < ActiveRecord::Migration

  def up
    remove_column :spree_users, :birthdate, :time
    add_column :spree_users, :birthdate, :date
  end

  def down
    remove_column :spree_users, :birthdate, :date
    add_column :spree_users, :birthdate, :time
  end
end

这篇关于为Spree Devise注册添加名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆