Rails 4 - participation of strong parameters in spree-2.1

How to add new fields for spree :: user in Spree-2.1 + Rails4?

Like my old setup: ===========================

Spree :: User.class_eval do

attr_accessible :f_name, :l_name :gender validates :f_name, :presence => true, :length => {:maximum => 25} validates :l_name, :presence => true, :length => {:maximum => 20} 

end

new work with strong parameters: =================================

Spree UserRegistrationsController.class_eval do module

 private def spree_user_params params.require(:spree_user).permit(:f_name, :l_name) end end 

end

Despite the fact that it does not work, because it received a rollback.

Please let me know your comments.

+6
source share
2 answers

The spree_auth_devise value has recently been changed to allow you to set the allowed attributes for Spree :: User.

This is the corresponding line of code: https://github.com/spree/spree_auth_devise/blob/ac27effb5998e5875261f08655e442461a031370/app/controllers/spree/user_registrations_controller.rb#L69

You will need to add f_name and l_name to Spree :: PermitedAttributes.user_attributes as follows:

 # in an initializer Spree::PermittedAttributes.user_attributes << :f_name Spree::PermittedAttributes.user_attributes << :l_name 

More information about Spree :: PermitedAttributes can be found in this pull request:

https://github.com/spree/spree/pull/3566

+13
source

@gmacdougall is right, however I want to note that if you do this in an extension, you can do it through a decorator. Your extension will need to define this in the initializer, which is set by the user through a generator that cannot be saved.

 # lib/spree/permitted_attributes_decorator.rb Spree::PermittedAttributes.class_eval do @@user_attributes.push(:f_name, :l_name) end 

You can add new attributes for many models in the Spree module : PermitedAttributes . Spree controllers receive these attributes using the methods included in the Spree :: Core :: ControllerHelpers :: StrongParameters module .

+1
source

Source: https://habr.com/ru/post/957889/


All Articles