2017-01-31 59 views
0

我有一個模型Actes和一個模型是彼此相關的habtm關鍵字。通過強大的參數獲取嵌套屬性

將新的acte表單發送到服務器時,我無法通過強參數。

這是params[:acte]

"acte"=>{ 
     "biblio_id"=>"1", 
     "acte_numero"=>"12", 
     "keywords_attributes"=>{"0"=>{"keyword"=>"attestation, "}}, 
     "texte"=>"<p>test</p>", 
     "complement"=>"" 
     } 

模型acte.rb包含:

has_and_belongs_to_many :keywords 
belongs_to :user, optional: true 
belongs_to :biblio, optional: true 
belongs_to :archive, optional: true 
has_many :comments 

accepts_nested_attributes_for :keywords, allow_destroy: true, 

注:如果我添加reject_if: :all_blankaccepts_nested_attributes_for,表單沒有得到根本發送。

這是acte_controller:

def params_acte 
    params.require(:acte).permit(:biblio_id, 
           :acte_numero, 
           :keywords_attributes =>[:keyword], 
           : resume, 
           # cut for brievity 

這引發一個syntax error, unexpected ',', expecting => :resume, ^

回答

2

你應該在最後添加keywords_attributes

def params_acte 
    params.require(:acte).permit(:biblio_id, 
           :acte_numero, 
           :resume, 
           # All other model attributes 
           :keywords_attributes =>[:keyword], 

這將工作。

注意:總是在結束

添加嵌套屬性