问题
I have an association as so:
User has many Uploads Uploads belongs to User and has many Upload Images Upload Images belong to uploads <-- Grandchild
I create my User before I add Uploads to that User. Uploads are stand-alone files of misc type, but they can also have many Upload Images linked to them to better describe the Upload (hence the seperate Upload Image model). Right now the Upload and Upload_Image persistence takes place in the users update function as they're nested form attributes.
Because Uploads can have many Upload_Images I'm trying to use Uploadify to persist the images but I can't get it sent along with the rest of the params hash to the Users update function (I used to be able to when it was just a single paperclip image upload being sent). Uploadify now forces me to send the Upload_Image params to a separate create action, which shouldn't be a problem as I can simply calculate the upload_id foreign key and manually assign it.
The problem however is that when I perform a save it leaves the auto-generated fields i.e id and timestamps as nils, how can I prevent this?
Here's my models(simplified):
User.rb
has_many :uploads, :dependent => :destroy, :order => 'created_at desc'
accepts_nested_attributes_for :uploads, :allow_destroy => true
Upload.rb
belongs_to :user
has_many :upload_images, :dependent => :destroy
accepts_nested_attributes_for :upload_images
has_attached_file :upload, :path => ":rails_root/:class/:id/:basename.:extension", :url => ":rails_root/:class /:id/:basename.:extension"
UploadImage.rb
belongs_to :upload
has_attached_file :image, :styles => {:thumb => "125x125#", :small => "150x150>", :medium => "200x200>", :large => "320x240>"},
:path => ":rails_root/uploads/:upload_id/:class/:id/:style/:basename.:extension",
:url => ":rails_root/uploads/:upload_id/:class/:id/:style/:basename.:extension"
Extra
Here's my uploadify script:
<script type="text/javascript" charset="utf-8">
<%- session_key = Rails.application.config.session_options[:key] -%>
$(document).ready(function() {
// Create an empty object to store our custom script data
var uploadify_script_data = {};
// Fetch the CSRF meta tag data
var csrf_token = $('meta[name=csrf-token]').attr('content');
var csrf_param = $('meta[name=csrf-param]').attr('content');
// Now associate the data in the config, encoding the data safely
uploadify_script_data[csrf_token] = encodeURI(encodeURI(csrf_param));
// Now associate the data in the config, encoding the data safely
uploadify_script_data[csrf_token] = encodeURI(csrf_param)
$('.uploadify').uploadify(
{
uploader : '/uploadify/uploadify.swf',
cancelImg : '/uploadify/cancel.png',
multi : true,
auto : true,
script : '/uploads',
onComplete : function(event, queueID, fileObj, response, data)
{
var dat = eval('(' + response + ')');
$.getScript(dat.upload);
},
scriptData :
{
'_http_accept': 'application/javascript',
'format' : 'json',
'_method': 'post',
'<%= session_key %>' : encodeURIComponent('<%= u cookies[session_key] %>'),
'authenticity_token': encodeURIComponent('<%= u form_authenticity_token %>'),
'upload_id' : '<%= Upload.last.id + 1 %>'
}
});
$('#submit').click(function(event){
event.preventDefault();
});
$('#submit').click(function(event){
event.preventDefault();
$('.uploadify').uploadifyUpload();
});
});
</script>
And the create action:
def create
@upload_image_test = UploadImage.new(:image => params[:Filedata])
@upload_image_test.upload_id = Upload.last.id + 1
end
回答1:
This was more of a design issue than anything. Basically I was attempting to create a child object before the parent so I simply needed to move this form to a separate page that takes place after the parent was created.
来源:https://stackoverflow.com/questions/10606286/rails-3-uploadify-paperclip-cant-auto-generate-grandchild-record-id-on-save