我正在尝试使用 configure_permitted_parameters 在注册时保存嵌套属性。我做了
在航线上
devise_for :students , controllers: { registrations: "registrations" }
学生模型:
class Student < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :highlights
accepts_nested_attributes_for :highlights #,:reject_if => proc { |a| a['name'].blank? }, allow_destroy: true
end
跨行注册控制器
class RegistrationsController < Devise::RegistrationsController
before_action :configure_permitted_parameters, :only => [:create]
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:email, :password, :first_name, :last_name ,:college_ids, highlights_attributes: [:id, :name, :_destroy], adjectives_attributes: [:id, :name, :_destroy]) }
end
end
可见
#highlights
= f.simple_fields_for :highlights, (f.object.highlights.build unless f.object.highlights.present?) do |highlight|
= render 'highlight_fields', f: highlight
.example
.col-xs-12
= link_to_add_association f, :highlights, class:"btn btn-dark waves-effect waves-light" do
i.icon.md-plus
span.hidden-xs Add More Highlights
突出部分
.highlight.nested-fields
.example
.col-md-11
= f.input :name,required:true, wrapper: :base_input,input_html:{value: f.object.name }, label:'NAME', placeholder:'Name here'
.col-sm-1
h4.example-title
= link_to_remove_association f, class:'btn btn-icon btn-flat btn-default red-600' do
i.icon.md-delete
现在,当我尝试保存的参数超过收到的参数时
<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"z+ue8ewIcnZY3OUFQI8sKq4yNbmc0Zi8w5Jdiioov6A3tWRxS9+r8lyqbOahzo+9ftWS06JtKBYDDyXLtOwAEQ==", "student"=><ActionController::Parameters {"first_name"=>"Muhammad", "last_name"=>"Iqbal", "email"=>"m.faisal.pucit@gmail.com", "password"=>"12345678", "password_confirmation"=>"12345678", "highlights_attributes"=>{"0"=>{"name"=>"ada", "_destroy"=>"false"}}, "adjectives_attributes"=>{"0"=>{"name"=>"asdasd", "_destroy"=>"false"}}} permitted: false>, "controller"=>"registrations", "action"=>"create"} permitted: false>
但是它只为学生保存名字和姓氏,但不保存名字。
有人能帮我我错过的事情吗?
通话记录
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"z+ue8ewIcnZY3OUFQI8sKq4yNbmc0Zi8w5Jdiioov6A3tWRxS9+r8lyqbOahzo+9ftWS06JtKBYDDyXLtOwAEQ==", "student"=>{"first_name"=>"Muhammad", "last_name"=>"Iqbal", "email"=>"test@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "highlights_attributes"=>{"0"=>{"name"=>"ada", "_destroy"=>"false"}}, "adjectives_attributes"=>{"0"=>{"name"=>"asdasd", "_destroy"=>"false"}}}}