編集/更新アクションが機能しません。
私は3つのモデルを持っています:
ドキュメント.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を使用すると、作成はできますが、編集/更新はできません
作成のバージョン 2 を使用すると、作成、編集、更新ができません。
よろしく。