Ich habe Probleme mit has_many/has_many Beziehung verschachtelten Formular für den folgenden Fall verwenden.Keine Verbindung gefunden für Name - verschachtelte Formular-Rails 4
Ich habe Artikel und das kann viele Einheiten (Kg, Taschen, etc) haben. Und über mein Formular muss ich diese Daten aufzeichnen.
So entschied ich mich verschachtelte Form mit Beziehungen has_many zu verwenden, aber ich habe einen Fehler: ArgumentError (No association found for name 'article_unit_of_measurements'. Has it been defined yet?)
Dies ist mein Code der Klassen:
class Article < ActiveRecord::Base
has_many :unit_of_measurements, :through => :article_unit_of_measurements
accepts_nested_attributes_for :article_unit_of_measurements
end
class ArticleUnitOfMeasurement < ActiveRecord::Base
belongs_to :article
belongs_to :unit_of_measurement
accepts_nested_attributes_for :unit_of_measurement
end
class UnitOfMeasurement < ActiveRecord::Base
has_many :articles, :through => :article_unit_of_measurements
end
Und in meiner Form jquery Plugin select2.js mit um meine Auswahleinheiten als Tags anzuzeigen.
= simple_form_for([:shop, @article], html: {class: 'form-horizontal' }) do |f|
.col-md-10
= f.input :name, placeholder: "Name", :input_html => {:class => 'form-control'}, label: false
%label.col-md-2.control-label{for: "unit_of_measurement_id"} Unidad de medida
.col-md-10
%select#unit-select-measure.select2{:name =>"article[article_unit_of_measurements_attributes][#{Time.now.to_s}][unit_of_measurement_id]", :style => "width:100%;;padding: 0;border: none;", :multiple => true}
- @unitOfMeasurement.each do |unit|
%option{:value => "#{unit.id}"}
= unit.name
%button.btn.btn-primary{type: "submit"} Guardar
:javascript
$('#unit-select-measure').select2();
Und das ist mein Controller:
class Shop::ArticlesController < ApplicationController
def new
@article = Article.new
@unitOfMeasurement = UnitOfMeasurement.all
render layout: false
end
def create
article = Article.new(article_parameters)
if article.save
flash[:notice] = "Lorem ipsum dolor sit amet."
redirect_to :action => :index
else
flash[:error] = "Lorem ipsum dolor sit amet."
redirect_to :action => :index
end
end
private
def article_parameters
params.require(:article).permit(:name, article_unit_of_measurements_attributes: [:id, :article_id, :unit_of_measurement_id])
end
end
P. D: Dateneinheiten bereits ausgefüllt
Wurde dieses Problem bereits gelöst? – HackerKarma