मेरे पास रेल में तीन-स्तर का बहु-घोंसला वाला रूप है। सेटअप इस तरह है: परियोजनाओं में कई मील का पत्थर हैं, और मील के पत्थर में कई नोट्स हैं। लक्ष्य जावास्क्रिप्ट के साथ पृष्ठ के भीतर सब कुछ संपादन योग्य है, जहां हम पृष्ठ के भीतर एक परियोजना में कई नए मील का पत्थर जोड़ सकते हैं, और नए और मौजूदा मील के पत्थर में नए नोट्स जोड़ सकते हैं।रेलों में बहु-स्तर के नेस्टेड रूपों को "गंदे" के रूप में चिह्नित करना
सब कुछ उम्मीद के रूप में काम करता है, सिवाय इसके कि जब मैं (नए मील के पत्थर ठीक से काम जब उनमें नोट जोड़कर) एक मौजूदा मील का पत्थर के लिए नया नोट जोड़ने, नए नोट को बचाने नहीं होगा जब तक मैं क्षेत्रों के किसी भी है कि वास्तव में संपादित "गंदे"/संपादित रूप को चिह्नित करने के लिए मील का पत्थर से संबंधित है।
क्या मील का पत्थर ध्वजांकित करने का कोई तरीका है ताकि जोड़े गए नए नोट्स को बचाया जा सके?
संपादित करें: माफ करना, यह कोड के सभी में पेस्ट करने के लिए मुश्किल है क्योंकि वहाँ इतने सारे भागों, लेकिन यहाँ जाता है:
मॉडल
class Project < ActiveRecord::Base
has_many :notes, :dependent => :destroy
has_many :milestones, :dependent => :destroy
accepts_nested_attributes_for :milestones, :allow_destroy => true
accepts_nested_attributes_for :notes, :allow_destroy => true, :reject_if => proc { |attributes| attributes['content'].blank? }
end
class Milestone < ActiveRecord::Base
belongs_to :project
has_many :notes, :dependent => :destroy
accepts_nested_attributes_for :notes, :allow_destroy => true, :allow_destroy => true, :reject_if => proc { |attributes| attributes['content'].blank? }
end
class Note < ActiveRecord::Base
belongs_to :milestone
belongs_to :project
scope :newest, lambda { |*args| order('created_at DESC').limit(*args.first || 3) }
end
मैं एक jQuery आधारित उपयोग कर रहा हूँ, ऐसा करने के लिए रयान बेट्स के कॉम्बो सहायक/जेएस कोड का अविभाज्य संस्करण।
आवेदन हेल्पर
def add_fields_for_association(f, association, partial)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(partial, :f => builder)
end
end
मैं एक छिपा div में सहयोग के लिए प्रपत्र प्रस्तुत करना, और फिर इसे खोजने के लिए और के रूप में की जरूरत है इसे जोड़ने के लिए निम्न JavaScript का उपयोग करें।
जावास्क्रिप्ट
function addFields(link, association, content, func) {
var newID = new Date().getTime();
var regexp = new RegExp("new_" + association, "g");
var form = content.replace(regexp, newID);
var link = $(link).parent().next().before(form).prev();
if (func) {
func.call();
}
return link;
}
मैं कोड का केवल अन्य प्रासंगिक टुकड़ा है कि मैं के बारे में सोच सकते हैं अनुमान लगा रहा हूँ NotesController में विधि बनाने होगा:
def create
respond_with(@note = @owner.notes.create(params[:note])) do |format|
format.js { render :json => @owner.notes.newest(3).all.to_json }
format.html { redirect_to((@milestone ? [@project, @milestone, @note] : [@project, @note]), :notice => 'Note was successfully created.') }
end
end
@owner इवर फिल्टर से पहले निम्नलिखित में बनाया गया है:
def load_milestone
@milestone = @project.milestones.find(params[:milestone_id]) if params[:milestone_id]
end
def determine_owner
@owner = load_milestone || @project
end
बात यह है कि, यह सब ठीक काम करने लगता है, सिवाय इसके कि जब मैं मौजूदा मील के पत्थर में नए नोट्स जोड़ रहा हूं। नए नोट्स को बचाने के लिए मील का पत्थर "छुआ" होना चाहिए, या फिर रेल ध्यान नहीं देंगे।
मील का पत्थर 'accept_nested_attributes_for: नोट्स? कुछ कोड भी उपयोगी हो सकता है। – Anurag
कोड ऊपर जोड़ा गया! :) – simplesessions