Rails 4 - strong parameters concept involvement in spree-2.1
Asked Answered
C

2

6

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

Like my old customization: ==========================

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: ================================

module Spree UserRegistrationsController.class_eval do

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

end

end

Even though its not working as it got rollback..

Please let me know your comments.

Controversial answered 12/11, 2013 at 8:52 Comment(2)
Please format your code using 4 space indentation.Allbee
its giving the Unpermitted parameters: email, password, password_confirmation. i hope those are permitted in actual spree_auth_devise- registration controller.Controversial
S
13

A change was recently made to spree_auth_devise which will allow you to set permitted attributes for Spree::User.

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

You'll need to add f_name and l_name to Spree::PermittedAttributes.user_attributes like so:

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

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

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

Spectroscopy answered 15/11, 2013 at 15:58 Comment(1)
Spree::PermittedAttributes.user_attributes.push :first_name, :last_nameControversial
V
1

@gmacdougall is right, however I want to note that if you're doing this in an extension, you may want to do this via a decorator. Your extension would have to define this in an initializer that is installed by the user via a generator, which cannot be maintained.

# 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:PermittedAttributes module. Spree controllers obtain these attributes via the methods included by the Spree::Core::ControllerHelpers::StrongParameters module.

Volumed answered 20/4, 2014 at 1:40 Comment(2)
how do you verify this approach works? for the initializer approach you can rails c 2.1.2 :003 > Spree::PermittedAttributes.user_attributes => [:email, :password, :password_confirmation, :firstname, :lastname] however, with the decorator approach you mention the attributes do not showAttached
How to override view of registration view for that?Middle

© 2022 - 2024 — McMap. All rights reserved.