当我搜索时,这是一个常见问题,但我发现没有一个答案适合我的情况。我已经使用设计建立了一个用户模型,它有两个相关的模型,它有一个详细联系信息和许多地址。嵌套表单适用于地址,但未显示我的联系方式详细信息字段。
我的用户模型如下:
validates_presence_of :contact_detail, :addresses
# Include default devise modules. Others available are:
# :lockable, :timeoutable, :trackable and :omniauthable
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable
has_one :contact_detail, dependent: :destroy
has_many :addresses, dependent: :destroy
accepts_nested_attributes_for :addresses,
allow_destroy: true
accepts_nested_attributes_for :contact_detail,
allow_destroy: true
Run Code Online (Sandbox Code Playgroud)
联系方式型号只有belongs_to :user
我在我的应用程序控制器上进行了 devise gem 中提到的更改:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [addresses_attributes: [:street_name, :street_number, :city, :country, :postal_code, :name],
contact_detail_attributes: [:first_name, :last_name, :description, :telephone, :mobile ]])
end
end
Run Code Online (Sandbox Code Playgroud)
我的 …