本文介绍了Rails 4 - spree-2.1 中的强参数概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Spree-2.1 + Rails4 中为 spree::user 添加新字段?

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

就像我的旧定制:============================

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

狂欢::User.class_eval 做

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}

结束

具有强参数的新作品:==================================

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

模块狂欢UserRegistrationsController.class_eval 做

module Spree UserRegistrationsController.class_eval do

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

end

结束

即使它在回滚时不起作用..

Even though its not working as it got rollback..

请让我知道您的意见.

推荐答案

最近对 spree_auth_devise 进行了更改,允许您为 Spree::User 设置允许的属性.

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

这是相关的代码行: https://github.com/spree/spree_auth_devise/blob/ac27effb5998e5875261f08655e442461a031370/app/controllers/spree/user_registrations_controller.rb#L69

您需要像这样将 f_name 和 l_name 添加到 Spree::PermittedAttributes.user_attributes:

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

可以在此拉取请求中找到有关 Spree::PermittedAttributes 的更多信息:

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

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

这篇关于Rails 4 - spree-2.1 中的强参数概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 15:01