The edit/update actions doesn't work.
I have three models:
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
This is my new
method on documents_controller.rb:
def new
@document = Document.new
@all_documents = DocumentCatalog.all
@configuration_document = @document.configuration_documents.build
end
This is my create method on documents_controller.rb version 1 (With this code, I can create the document).
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
Using the code above for create, I have this error when I try to Edit:
undefined method `model_name' for nil:NilClass on `<%= fields_for (@configuration_document) do |dc|%>`
This is my code for create action on documents_controller version 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
Note: When I use the code above for the update, even the "new" doesn't work and I got this error on the create action:
NoMethodError - undefined method `reject' for "2":String:
params[:document_catalogs][:id] = params[:document_catalogs][:id].reject(&:empty?).map(&:to_i)
This is my update method on 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
If I leave the update like the normal scaffold. I get the error:
undefined method `model_name' for nil:NilClass on `<%= fields_for (@configuration_document) do |dc|%>
The relevant part of the "documents form":
<div class="form-group">
<%= fields_for (@configuration_document) do |dc|%>
<%= collection_select(:document_catalogs, :id, @all_documents, :id, :name, {}, class: 'form-control')%>
<% end %>
</div>
I really know that is a lot of things. But basically, If I use version one of "create", I can create but no edit/update
If I use version two of the create, I can't create, edit/update.
Regards.