【问题标题】:Store paperclip object temporarily in session在会话中临时存储回形针对象
【发布时间】:2013-11-20 04:48:38
【问题描述】:

我有三个字段的电子邮件、电话号码和发票

用户可以通过浏览按钮或拖放上传多张发票。

user 有两个字段:email 和 phone_number 用户有_many发票

javascript

<script type="text/javascript" charset="utf-8">
  $(function () {
      // Initialize the jQuery File Upload widget:
      $('#fileupload').fileupload({
        url: '/invoices/upload_file',
        change: function (e, data) {
        },
        add: function (e, data) {
            data.submit();
        }
      });
      // 
      // Load existing files:
  });


</script>

在这里,每当用户拖放文件时,我都会触发 ajax,而我正在将这个图像对象存储在会话中,但得到 ​​p>

无法转储错误

我的控制器

def new
    session[:invoices_attributes] ||= {}
    @invoice = User.new
    @invoice.invoices.build
end

def upload_file

    session["invoices_attributes"].deep_merge!( user_params["invoices_attributes"].first)
  render nothing: true
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4


    【解决方案1】:

    我认为,在会话中存储文件数据是不可能的。您应该尝试将其保存到文件系统上的临时文件中,然后在会话中存储该文件的路径。

    您可以使用保存临时文件

    # create temporary file
    temp_file = TempFile.new('invoice')
    # save uploaded file
    File.open(temp_file.path, "w") do |f|
      f.write params[:invoices_attributes].delete(:file).read
      f.close
    end     
    
    # store temporary file path
    params[:invoices_attributes][:file_path] = temp_file.path
    

    代码未经测试,但您应该有所了解。

    【讨论】:

      猜你喜欢
      • 2017-08-25
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 2012-01-26
      相关资源
      最近更新 更多