0

我有一个在 Heroku 上运行良好的应用程序,我正在尝试使用 Carrierwave 将该应用程序连接到亚马逊的 S3 服务。我以为我的配置文件都适用于所有内容,但是在创建图片实例时遇到了问题。我认为这与 Postgres 及其某些特定格式等有关,但这是主要问题。

我正在TypeError (can't convert nil into String):了解似乎是 POST 到数据库的内容(特别是在 create 方法上)。错误如下所示:

app/controllers/pictures_controller.rb:62:in `create'

TypeError (can't convert nil into String):
POST xxxxxxxx.herokuapp.com/684-536-025/pictures dyno=web.1 queue=0 wait=0ms service=510ms status=500 bytes=643

这也是 POST 的开始:

#<Picture id: nil, description: nil, image: nil, gallery_id: 15, gallery_url_id: "a432b35_21cc", original_filename: "Sunflower.gif", order_number: nil, created_at: nil, updated_at: nil>

我假设这里的问题是 Postgres 不允许在有字段为零时启动 POST。奇怪的是,sqlite 在这方面做得很好,这就是为什么我猜测问题是 Postgres,不幸的是我将使用 Postgres 进行生产。我对 Postgres 的了解还不够,无法真正缩小这个问题的范围,所以我想我会在这里询问是否可以得到任何帮助/建议!

谢谢!

编辑:这是额外帮助的创建方法

def create
# Creates a picture class instance based on the json objects being passed in from 
# Carrierwave
p_attr = params[:picture]
p_attr[:image] = params[:picture][:image].first if params[:picture][:image].class == Array

# Gets the gallery vai the uuid gallery url
@gallery = Gallery.where(:url_id => params[:url_id]).first
# Builds the picture based on the attributes passed in above from the json
@picture = @gallery.pictures.build(p_attr)
# Assigns the gallery_url_id attribute
@picture.gallery_url_id = @gallery.url_id

puts @picture.inspect

# Handle the picture save according to success or not
LINE 62 -> if @picture.save
  respond_to do |format|
    format.html {
      # Calls the method in the model to format the json repsonse
      render :json => [@picture.to_jq_upload].to_json,
      :content_type => 'text/html',
      :layout => false
    }
    format.json {
      render :json => [@picture.to_jq_upload].to_json
    }
  end
else
  render :json => [{:error => "custom_failure"}], :status => 304
end

结尾

代码中标记了第 62 行。

编辑2:这是所要求的模型......

class Picture < ActiveRecord::Base

  # The attributes accesible via an @picture
  attr_accessible :gallery_id, :description, :image, :gallery_url_id, :original_filename, :order_number
  belongs_to :gallery

  # Attatches the carrierwave uploader to the picture class
  mount_uploader :image, ImageUploader

  # Referenced when each picture is created, the json is formatted as the following form
  def to_jq_upload
    {
      "name" => read_attribute(:image),
      "size" => image.size,
      "url" => image.url,
      "thumbnail_url" => image.thumb.url,
      "delete_url" => id,
      "picture_id" => id,
      "delete_type" => "DELETE",
      "url_id" => read_attribute(:gallery_url_id),
      "original_filename" => read_attribute(:original_filename),
      "order_number" => ""
    }
  end

end
4

1 回答 1

0

我最终将其用作 config/initilizers/fog.rb

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => 'asdf',
    :aws_secret_access_key  => 'asdfadsf',
    :region                 => 'us-east-1'
  }
  config.fog_directory  = 'modea-design-comp-viewer'
end

此外,在控制器中,我必须手动将数据库中作为 nil 发布的两个字段设置为空字符串,如下所示:

def create
    # Creates a picture class instance based on the json objects being passed in from 
    # Carrierwave
    p_attr = params[:picture]
    p_attr[:image] = params[:picture][:image].first if params[:picture][:image].class == Array

    # Gets the gallery vai the uuid gallery url
    @gallery = Gallery.where(:url_id => params[:url_id]).first
    # Builds the picture based on the attributes passed in above from the json
    @picture = @gallery.pictures.build(p_attr)
    # Assigns the gallery_url_id attribute
    @picture.gallery_url_id = @gallery.url_id

    @picture.order_number = ""
    @picture.description = ""

    # Handle the picture save according to success or not
    if @picture.save
      puts @picture.inspect
      respond_to do |format|
        format.html {
          # Calls the method in the model to format the json repsonse
          render :json => [@picture.to_jq_upload].to_json,
          :content_type => 'text/html',
          :layout => false
        }
        format.json {
          render :json => [@picture.to_jq_upload].to_json
        }
      end
    else
      render :json => [{:error => "custom_failure"}], :status => 304
    end
  end

那是为我做的!

于 2012-07-16T21:01:36.820 回答