2012-07-16 90 views
0

我有一個應用程序在Heroku上運行良好,我試圖通過Carrierwave將應用程序連接到Amazon的S3服務。我以爲我有配置文件都適用於所有的東西,但是當我創建一個圖片實例時,我遇到了一個問題。我認爲這與Postgres及其特定格式等有關,但這是主要問題。Postgres + Heroku Ruby創建

我得到了一個TypeError (can't convert nil into String):看起來像POST到數據庫(特別是在創建方法)。錯誤如下所示:

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 
+0

你的圖片控制器中的第62行究竟是什麼,以及哪個字段是nil導致類型錯誤? app/controllers/pictures_controller.rb:62:在'create' – bento 2012-07-16 15:20:09

+0

我打算用create方法更新帖子,看它有幫助。它可能不得不處理屬性查找,雖然... – user1470511 2012-07-16 15:20:56

+0

我們可以看到一個圖片模型嗎? – Andrei 2012-07-16 15:45:48

回答

0

我結束了使用此作爲配置/ 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 

此外,在控制器I不得不手動設置,正在張貼零在數據庫中一個空字符串作爲這樣的兩個領域:

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 

這並它對我來說!