0

Herokuで正常に実行されているアプリがあり、Carrierwaveを使用してアプリをAmazonの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>

ここでの問題は、nilのフィールドがある場合にPostgresがPOSTの開始を許可しないことだと思います。奇妙なことに、sqliteはこれでうまく機能します。そのため、問題はPostgresであると推測していますが、残念ながら、本番環境ではPostgresを使用します。私はこの問題を本当に絞り込むためにPostgresについて十分に知らないので、私はここで助け/アドバイスを得ることができるかどうかを尋ねると思いました!

ありがとう!

編集:これが追加のヘルプのためのcreateメソッドです

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としてPOSTされていた2つのフィールドを、次のように空の文字列に手動で設定する必要がありました。

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 に答える