Tôi có một biểu mẫu ba cấp được lồng trong Rails. Các thiết lập là như thế này: Dự án có nhiều cột mốc quan trọng, và các mốc quan trọng có nhiều ghi chú. Mục đích là để mọi thứ có thể chỉnh sửa trong trang bằng JavaScript, nơi chúng tôi có thể thêm nhiều cột mốc mới vào một dự án trong trang và thêm Ghi chú mới cho các mốc mới và hiện có.Đánh dấu các biểu mẫu lồng nhau đa cấp dưới dạng "bẩn" trong Rails
Mọi thứ hoạt động như mong đợi, ngoại trừ khi tôi thêm ghi chú mới vào hiện tại Milestone (cột mốc mới hoạt động tốt khi thêm ghi chú cho chúng), ghi chú mới sẽ không lưu trừ khi tôi chỉnh sửa bất kỳ trường nào thực sự thuộc về cột mốc để đánh dấu mẫu "bẩn"/chỉnh sửa.
Có cách nào để gắn cờ mốc quan trọng để các Ghi chú mới đã được thêm vào sẽ lưu không?
Edit: xin lỗi, thật khó để dán vào tất cả các mã vì có rất nhiều nơi, nhưng ở đây đi:
Models
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
Tôi đang sử dụng jQuery-based, phiên bản không phô trương của mã combo/mã JS của Ryan Bates để hoàn thành công việc này.
Application Helper
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
tôi làm cho hình thức cho hiệp hội trong một div ẩn, và sau đó sử dụng JavaScript sau để tìm thấy nó và thêm nó khi cần thiết.
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;
}
Tôi đoán chỉ có mảnh khác có liên quan của mã mà tôi có thể nghĩ đến sẽ là tạo ra phương pháp trong 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
Các @owner Ivar được tạo trong các mục sau đây trước khi lọc:
def load_milestone
@milestone = @project.milestones.find(params[:milestone_id]) if params[:milestone_id]
end
def determine_owner
@owner = load_milestone || @project
end
Thing là, tất cả điều này dường như làm việc tốt, ngoại trừ khi tôi đang thêm ghi chú mới cho các cột mốc hiện có. Các mốc quan trọng phải được "xúc động" để ghi chú mới để tiết kiệm, hoặc người nào khác Rails sẽ không chú ý.
làm các mốc quan trọng 'accept_nested_attributes_for: notes'? cũng một số mã có thể hữu ích. – Anurag
Mã được thêm ở trên! :) – simplesessions