Postgres + Heroku Ruby Creation

时间:2012-07-16 14:58:19

标签: ruby-on-rails post heroku carrierwave rails-postgresql

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

我在数据库的POST(特别是创建方法)上得到TypeError (can't convert nil into String):。错误如下所示:

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

1 个答案:

答案 0 :(得分: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

此外,在控制器中,我必须手动将数据库中POST为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

那是为我做的!

相关问题