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 

をdocuments_controller.rbの210の方法:

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 

注:私は、更新のために上記のコードを使用して、でも「新しい」仕事と私が作成したアクションに、このエラーを得ていない:これはdocuments_controllerの私の更新方法である

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

.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を使用すると、編集はできません。

よろしくお願いいたします。

+1

「update_attributes」の動作が変更されていることに注意してください。どのバージョンのRailsを使用していますか? 'edit'アクションはどのように実装しましたか?また、あなたの質問を編集して、あなたが求めていることを本当に明確にしてください。 1つのバージョンのコードに減らし、分析のための確固たる基盤を確保してください。 – Raffael

答えて

0

はこれを試してみてください:

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

field_for方法は2つの引数、モデル、およびモデルオブジェクトを受け取ります。あなたはモデルを見逃していました。あなたがしている、第二create方法で

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

params[:document_catalogs][:id].each_line 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これがあなたの質問に答えたなら、それを受け入れる答えの隣にあるチェックマークをクリックしてください。チェックマークが緑色に変わります。アップホーテも常に高く評価されています。 :D –

関連する問題