def update
@folder = Folder.find(params[:id])
@folder.attributes = params[:folder]
add_new_file = false
delete_file = false
@folder.files.each do |file|
add_new_file = true if file.new_record?
delete_file = true if file.marked_for_destruction?
end
both = add_new_file && delete_file
if both
redirect_to "both_action"
elsif add_new_file
redirect_to "add_new_file_action"
elsif delete_file
redirect_to "delete_file_action"
else
redirect_to "folder_not_changed_action"
end
end
Sometimes you want to know that a folder has been changed without determining how. In this case, you can use the mode autosavein your association:
class Folder < ActiveRecord::Base
has_many :files, :autosave => true
accepts_nested_attributes_for :files
attr_accessible :files_attributes
end
Then in the controller you can use @folder.changed_for_autosave?that returns regardless of whether this record was changed in any way (new_record ?, marked_for_destruction ?, changed?), Including whether any of its nested autosave associations also changed.
Updated.
folder, e.q. @folder.how_changed?, : add_new_file,: delete_file .. ( , , ). .
case @folder.how_changed?
when :both
redirect_to "both_action"
when :add_new_file
redirect_to "add_new_file_action"
when :delete_file
redirect_to "delete_file_action"
else
redirect_to "folder_not_changed_action"
end
2 : new_record? marked_for_destruction? , Rails in-box changed_for_autosave? , - . .