7
다음 경우에 중첩 된 양식을 사용하여 has_many/has_many 관계에 문제가 있습니다.이름에 대한 연관이 없습니다. 중첩 된 양식 레일 4
나는 기사가 있고 이것에는 많은 단위 (Kg, 부대, 등등)가있을 수있다. 제 양식을 통해이 데이터를 기록해야합니다.
그래서 나는 관계 has_many와 중첩 된 형식을 사용하기로 결정하지만 오류가 있습니다
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
그리고 내 양식에
는 jQuery 플러그인의 select2.js를 사용 :이 클래스의 내 코드ArgumentError (No association found for name 'article_unit_of_measurements'. Has it been defined yet?)
입니다 내 선택 단위를 태그로 보여줍니다.
= 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();
그리고 이것은 내 컨트롤러 :
이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 : 데이터 단위가 이미
이 문제가 아직 해결 되었습니까? – HackerKarma