Rails 3 Uploadify + Paperclip无法在保存时自动生成孙记录ID [英] Rails 3 Uploadify + Paperclip Can't Auto-Generate Grandchild Record ID On Save

查看:93
本文介绍了Rails 3 Uploadify + Paperclip无法在保存时自动生成孙记录ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关联:

用户有很多上传 上载属于用户,并且有很多上载图像 上传图片属于上传<-孙子

User has many Uploads Uploads belongs to User and has many Upload Images Upload Images belong to uploads <-- Grandchild

在将上传内容"添加到该用户之前,我创建了我的用户.上载是杂项类型的独立文件,但是它们也可以链接许多上载图像,以更好地描述上载(因此使用单独的上载图像模型).现在,Upload和Upload_Image持久性发生在用户更新功能中,因为它们是嵌套的表单属性.

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.

因为上载可能有许多Upload_Image,所以我试图使用Uploadify来保留图像,但无法将其与其余的参数哈希一起发送到Users更新功能(我以前能够只是发送了一个回形针图像而已). Uploadify现在强制我将Upload_Image参数发送到单独的create动作,这应该不是问题,因为我可以简单地计算upload_id外键并手动分配它.

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.

但是问题是,当我执行保存操作时,它将自动生成的字段(即id和timestamps)保留为nil,如何防止这种情况?

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

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

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"

额外

这是我的uploadify脚本:

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>

创建操作:

  def create
    @upload_image_test = UploadImage.new(:image => params[:Filedata])
    @upload_image_test.upload_id = Upload.last.id + 1     
  end

推荐答案

这更多的是设计问题.基本上,我试图在父对象之前创建一个子对象,所以我只需要将此表单移动到创建父对象之后发生的单独页面.

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.

这篇关于Rails 3 Uploadify + Paperclip无法在保存时自动生成孙记录ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆