0

編輯/更新操作不起作用。Rails編輯/更新操作不適用於嵌套模型(has_many:through)

我有三個型號:

document.rb

belongs_to :languages 
has_many :configuration_documents 
has_many :document_catalogs, through: :configuration_documents 

accepts_nested_attributes_for :document_catalogs 
accepts_nested_attributes_for :configuration_documents 

document_catalog.rb

has_many :configuration_documents 
has_many :documents, through: :configuration_documents 

configuration_document.rb

belongs_to :document 
belongs_to :document_catalog 

這是我new方法上documents_controller.rb:

def new 
    @document = Document.new 
    @all_documents = DocumentCatalog.all 
    @configuration_document = @document.configuration_documents.build 
    end 

這是我創建documents_controller.rb版本1法(有了這個代碼,我可以創建文檔)。

def create 
    @document = Document.new(document_params) 

    params[:document_catalogs][:id].each_line do |document| 
     if !document.empty? 
     @document.configuration_documents.build(:document_catalog_id => document) 
     end 
    end 

    respond_to do |format| 
     if @document.save 
     format.html { redirect_to admin_document_path(@document), notice: 'Document was successfully created.' } 
     format.json { render :show, status: :created, location: @document } 
     else 
     format.html { render :new } 
     format.json { render json: @document.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

使用上面的代碼創建,我有這樣的錯誤,當我嘗試編輯

undefined method `model_name' for nil:NilClass on `<%= fields_for (@configuration_document) do |dc|%>` 

這是我對documents_controller 2版創建動作代碼:

def create 
    @document = Document.new(document_params) 
    if params[:document_catalogs][:id] 
     params[:document_catalogs][:id] = params[:document_catalogs][:id].reject(&:empty?).map(&:to_i) 
     params[:document_catalogs][:id].each do |document| 
     @miniature.configuration_documents.build(:document_catalogs_id => document) 
     end 
    end 

    respond_to do |format| 
     if @document.save 
     format.html { redirect_to admin_document_path(@document), notice: 'Document was successfully created.' } 
     format.json { render :show, status: :created, location: @document } 
     else 
     format.html { render :new } 
     format.json { render json: @document.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

注:當我使用上面的代碼進行更新,即使是「新」不工作,我上了創建操作此錯誤:

NoMethodError - undefined method `reject' for "2":String: 
params[:document_catalogs][:id] = params[:document_catalogs][:id].reject(&:empty?).map(&:to_i) 

這是documents_controller我的更新方法.RB:

def update 
    @document = Document.find(params[:id]) 
    if params[:document_catalogs][:id] 
     params[:document_catalogs][:id] = params[:document_catalogs][:id].reject(&:empty?).map(&:to_i) 
     old_documents_catalogs = @document.configuration_documents.pluck(:document_catalog_id) 
     new_dcos = params[:document_catalogs][:id] - old_documents_catalogs 
     old_documents_catalogs = old_documents_catalogs - params[:document_catalogs][:id] 
     new_dcos.each do |dc| 
     @document.configuration_documents.build(:document_catalog_id => dc) 
     end 
     ConfigurationDocument.delete_all(:document_catalog_id => old_documents_catalogs) 
    end 
    if @document.update_attributes(document_params) 
     flash[:success] = "document updated" 
     redirect_to @document 
    else 
     render 'edit' 
    end 
end 

如果我離開就像普通的支架更新。我得到的錯誤:

undefined method `model_name' for nil:NilClass on `<%= fields_for (@configuration_document) do |dc|%> 

「文檔格式」的相關部分:

<div class="form-group"> 
     <%= fields_for (@configuration_document) do |dc|%> 
     <%= collection_select(:document_catalogs, :id, @all_documents, :id, :name, {}, class: 'form-control')%> 
    <% end %> 
    </div> 

我真的知道這是一個很大的事情。但基本上,如果我使用「創建」版本之一,我可以創建但不能編輯/更新

如果我使用創建的第二版,我無法創建,編輯/更新。

問候。

+1

請注意'update_attributes'的行爲已經改變。你使用的是什麼版本的Rails?你是如何實現編輯行爲的?另外,請編輯你的問題,使其真正清楚你在問什麼。減少到一個版本的代碼,所以我們有一個堅實的分析基礎。 – Raffael

回答

0

嘗試這種情況:

<%= fields_for :configuration_document, @configuration_document do |dc|%> 

field_for方法需要兩個參數,所述模型,和所述模型對象。你只是錯過了模型。

在你的第一個create方法,你使用這樣的:

params[:document_catalogs][:id].each_line do |document| 

,我相信你真正想要的是這樣的:

params[:document_catalogs][:id].each do |document| 

在第二create方法,你使用此:

params[:document_catalogs][:id] = params[:document_catalogs][:id].reject(&:empty?).map(&:to_i) 
params[:document_catalogs][:id].each do |document| 

我覺得你真的想用這個:

params[:document_catalogs].reject(&:empty?).map(&:to_i).each do |document| 
+0

@CharlesMagnussen如果這回答了您的問題,請點擊答案旁邊的複選標記以接受它。複選標記會將顏色更改爲綠色。 Upvotes也是非常值得讚賞的。 :d –

相關問題