编辑/更新操作不起作用。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>
我真的知道这是一个很大的事情。但基本上,如果我使用“创建”版本之一,我可以创建但不能编辑/更新
如果我使用创建的第二版,我无法创建,编辑/更新。
问候。
请注意'update_attributes'的行为已经改变。你使用的是什么版本的Rails?你是如何实现编辑行为的?另外,请编辑你的问题,使其真正清楚你在问什么。减少到一个版本的代码,所以我们有一个坚实的分析基础。 – Raffael