I am working on a product admin section. I have productvariants
, which can have many productattributevalues
through productvariantdetails
.
productattributevalues
(green, yellow, S, XL) belongs_to productattributes
where I have specified attributes such as: color, size and material.
I would like to have a nested form on my productvariants/_form
where I can choose (optionally) if a productvariant
has an productattributevalue
of: size or color applied.
I changed my models and introduced two aliases for productattributevalues
. One for each attribute.
class Productvariant < ActiveRecord::Base
has_many :productattributevalues
has_many :productvariantdetails
has_many :productattributevalues, :through => :productvariantdetails
has_many :productattributecolors, :through => :productvariantdetails, :source => :productattributevalue
has_many :productattributesizes, :through => :productvariantdetails, :source => :productattributevalue
accepts_nested_attributes_for :productvariantdetails, :allow_destroy => true
end
class Productattributevalue < ActiveRecord::Base
attr_accessible :value, :productattribute_id
scope :product_color, joins(:productattribute).where('productattributes.attributename' => 'color')
scope :product_size, joins(:productattribute).where('productattributes.attributename' => 'size')
belongs_to :productattribute
end
This is my view
<%= form_for [:admin, @productvariant], :html => {'role' => 'form' } do |f| %>
.
.
.
<%= f.fields_for : productattributecolors do |ff| %>
<%= ff.label "Productattribute: Color" %>
<%= ff.select(:productattributecolor, Productattributevalue.product_color.uniq.order('id asc').map{|s| [s.value, s.id]}, {:selected => params[:productattributecolor], :include_blank => false, :prompt => "Select color"}, {}) %>
<% end %>
<%= f.fields_for : productattributesizes do |ff| %>
<%= ff.label "Productattribute: Size" %>
<%= ff.select(:productattributesize, Productattributevalue.product_size.uniq.order('id asc').map{|s| [s.value, s.id]}, {:selected => params[:productattributesize], :include_blank => false, :prompt => "Select size"}, {}) %>
<% end %>
.
.
.
<%= f.submit "Save" %>
<% end %>
How can I get this working?
Thank you in advance!
Aucun commentaire:
Enregistrer un commentaire