2016-10-26 2 views
0

Ich muss ein benutzerdefiniertes Feld in der Join-Tabelle für Viele festlegen. Momentan kann ich nur Relationen mit Checkboxen setzen, aber ich muss auch ein Mengenfeld setzen.Formtastic-benutzerdefiniertes Feld in der Join-Tabelle

Meine Modelle:

class Quote < ApplicationRecord 
    has_and_belongs_to_many :options, :join_table => :quotes_options 
    accepts_nested_attributes_for :options, :allow_destroy => true 
end 

class Option < ApplicationRecord 
    has_and_belongs_to_many :quotes 
end 

und Form:

form do |form| 
    form.inputs do 
     form.input :options, :as => :check_boxes,:collection => Option.all 
    end 
end 

So, jetzt ist es als Liste der Kontrollkästchen wie

New Quote 
[X] First option 
[ ] Second option 

Die Frage gemacht ist, dass in der Tabelle „: join_table =>: quotes_options "Ich habe auch Feld ": Menge ", also würde ich es mögen e, um es auch zu aktualisieren. So wird meine Ansicht wie sein und ich werde Menge sparen in gemeinsamen Tabelle in der Lage

New Quote 
[X] First option [  ] quantity 
[ ] Second option  [  ] quantity 

Antwort

0

Wenn jemand interessiert Ich erkannte, dass es wie folgt aus:

form.inputs do 
     Option.all.each do |option| 
     form.semantic_fields_for :quote_options, form.object.quote_options.detect_or_build_by_quote(option) do |quote_option| 
      quote_option.input :option_name_selected , :as => :boolean, :label => option.name 
      quote_option.input :quantity , :as => :number 
      quote_option.input :option_id , :as => :hidden 
     end 
     end 

Quote Modell

has_many :quote_options do 
    def detect_or_build_by_quote(option) 
     record = self.detect{ |quote_option| quote_option.option_id == option.id } 
     if record.nil? 
     record = self.new(:option_id => option.id) 
     end 
     record 
    end 
    end 

    has_many :options, :join_table => :quote_options 
    accepts_nested_attributes_for :options, :allow_destroy => true   
    accepts_nested_attributes_for :quote_options , reject_if: proc { |attributes| attributes['quantity'].blank? or attributes['quantity'].to_f.zero? } 
Verwandte Themen