【问题标题】:Rails send_data with oracle enhanced adapter带有 oracle 增强适配器的 Rails send_data
【发布时间】:2013-07-14 23:21:37
【问题描述】:

我在检索作为 blob 存储在旧版 Oracle 数据库中的图像时遇到问题。当我转到 http://server/images/id/type 时,我收到 no implicit conversion of Symbol into Integer 错误。

我有以下设置:

宝石文件

gem 'rails', '3.2.13'
gem 'activerecord-oracle_enhanced-adapter'
gem 'ruby-oci8'

表格

CREATE TABLE "SCHEMA"."IMAGE_TABLE" 
(   "IMG_TYPE_SEQ_NO" NUMBER(12,0), 
"IMG_TYPE" VARCHAR2(10 BYTE), 
"IMG_DESC" VARCHAR2(60 BYTE), 
"IMG_LENGTH" NUMBER, 
"IMG_BLOB" BLOB
);

型号

class Image < ActiveRecord::Base
self.table_name = 'schema.image_table'

def self.image_by_id_and_type(id, type)
  where(
    'img_type_seq_no = :id and img_type = :type',
     id: id, type: type
  )
end

控制器

class ImagesController < ApplicationController

  def show_image
    @image = Image.image_by_id_and_type(params[:id], params[:type])
    send_data @image[:img_blob], type: 'image/gif', disposition: 'inline'
  end

end

我尝试使用send_data @image.img_blob 并得到undefined method 错误。

我做错了什么?

谢谢, 克里斯

更新:

不知道是不是类型转换的问题。 Blob 图像是通过一个将其转换为 java 字节数组的 java swing 应用程序保存的。这可能是问题吗?如果是这样,我如何将 java 字节数组转换为 send_data 可以理解的东西?

【问题讨论】:

    标签: ruby-on-rails oracle activerecord


    【解决方案1】:

    这是一个疯狂的猜测。请尝试

    def show_image
        @image = GiftCardImage.image_by_id_and_type(params[:id].to_i, params[:type])
        send_data @image[:img_blob], type: 'image/gif', disposition: 'inline'
    end
    

    在你的模型中

    class Image < ActiveRecord::Base
    self.table_name = 'schema.image_table'
    
    def self.image_by_id_and_type(id, type)
      where("img_type_seq_no = ? and img_type = ?", id, type)
    end
    

    【讨论】:

    • 我修改了上面的示例以删除可能引起混淆的域特定名称。我在尝试对其进行泛化时错过了它们。

      您的建议虽然看起来更干净,但没有奏效。我收到了同样的错误信息。
    【解决方案2】:
    @image = Image.image_by_id_and_type(params[:id], params[:type])
    

    正在返回一个 ActiveRecord::Relation 对象,因此为了从中获取实际值,我必须调用 first

    send_data @image.first[:img_blob], type: 'image/gif', disposition: 'inline'
    

    【讨论】:

      猜你喜欢
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      相关资源
      最近更新 更多