Adding name to Spree Devise registration

2019-07-22 02:46发布

问题:

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.

回答1:

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


回答2:

you can run command in terminal:

rails generate migration add_name_to_spree_users name:string

rails generate migration add_sur_name_to_spree_users sur_name:string

rails generate migration add_birthdate_to_spree_users birthdate:date

rake db:migrate

than Inside config/initializers/spree.rb

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

than Restart Your server