0

我有两个模型:视频和图像。

创建新视频时,我想保存视频缩略图的图像。我试图在我的视频控制器“创建”操作中使用以下几行:

newImage = Image.new(:step_id=>@video.step_id, :imagePath=>@video.thumbnail_url, :project_id=>@video.project_id, :saved=> true, :position=>position).save
logger.debug "newImage #{newImage}"

现在,图像似乎没有保存,我不确定我做错了什么。这是我收到的错误消息:

ActiveRecord::RecordInvalid (Validation failed: Imagepath can't be blank):
  app/controllers/videos_controller.rb:35:in `create'


  Rendered /Users/ttseng/.rvm/gems/ruby-1.9.3-p448/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
  Rendered /Users/ttseng/.rvm/gems/ruby-1.9.3-p448/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
  Rendered /Users/ttseng/.rvm/gems/ruby-1.9.3-p448/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (7.3ms)

但是,我的日志清楚地表明我没有传递空白的 imagePath(见下文)。

这是我对视频的控制器操作:

# POST /videos
  def create
    # need to validate
    @video = Video.create(params[:video])
    # get the thumbnail image
    thumbnail = @video.thumb_url
    logger.debug "thumbnail video: #{thumbnail}"
    @video.update_attributes(:thumbnail_url => thumbnail)

    # create a new image record for the thumbnail
    logger.debug "creating new image"
    logger.debug "@video.step_id: #{@video.step_id}"
    position = Step.find(@video.step_id).images.count # set the position of the added thumbnail to the last
    logger.debug "position: #{position}"
    logger.debug "imagePath: #{@video.thumbnail_url}"
    logger.debug "project_id: #{@video.project_id}"
    newImage = Image.new(:step_id=>@video.step_id, :imagePath=>@video.thumbnail_url, :project_id=>@video.project_id, :saved=> true, :position=>position).save
    logger.debug "newImage #{newImage}"

    respond_to do |format|
      if @video.save        
        format.js
      else
        format.json { render :json => @video.errors, :status => :unprocessable_entity }
      end
    end
  end

这将返回以下日志:

Started POST "/videos" for 127.0.0.1 at 2013-07-05 14:09:45 -0400
Processing by VideosController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"e5YFBA3CSbeUiVgsimzrw2DlMrQbWNZmMfpYJoGLNCY=", "video"=>{"project_id"=>"108", "step_id"=>"523", "saved"=>"true", "url"=>"http://youtu.be/O9k-MsfIkMY"}, "button"=>""}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
   (0.2ms)  BEGIN
video id: O9k-MsfIkMY
  SQL (0.9ms)  INSERT INTO "videos" ("created_at", "position", "project_id", "saved", "step_id", "thumbnail_url", "updated_at", "url") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["created_at", Fri, 05 Jul 2013 14:09:45 EDT -04:00], ["position", nil], ["project_id", 108], ["saved", true], ["step_id", 523], ["thumbnail_url", nil], ["updated_at", Fri, 05 Jul 2013 14:09:45 EDT -04:00], ["url", "http://youtu.be/O9k-MsfIkMY"]]
   (0.4ms)  COMMIT
video id: O9k-MsfIkMY
thumbnail_url: http://img.youtube.com/vi/O9k-MsfIkMY/default.jpg
thumbnail video: http://img.youtube.com/vi/O9k-MsfIkMY/default.jpg
   (0.1ms)  BEGIN
video id: O9k-MsfIkMY
   (0.3ms)  UPDATE "videos" SET "thumbnail_url" = 'http://img.youtube.com/vi/O9k-MsfIkMY/default.jpg', "updated_at" = '2013-07-05 18:09:45.952229' WHERE "videos"."id" = 41
   (0.4ms)  COMMIT
creating new image
@video.step_id: 523
  Step Load (0.3ms)  SELECT "steps".* FROM "steps" WHERE "steps"."id" = $1 LIMIT 1  [["id", 523]]
   (0.4ms)  SELECT COUNT(*) FROM "images" WHERE "images"."step_id" = 523
position: 0
imagePath: http://img.youtube.com/vi/O9k-MsfIkMY/default.jpg
project_id: 108
   (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
newImage false
   (0.1ms)  BEGIN
video id: O9k-MsfIkMY
   (0.1ms)  COMMIT
video id: O9k-MsfIkMY
  Rendered videos/create.js.erb (0.3ms)
Completed 200 OK in 26ms (Views: 5.7ms | ActiveRecord: 4.0ms)

视频.rb

class Video < ActiveRecord::Base

  attr_accessible :position, :project_id, :saved, :step_id, :url, :thumbnail_url

  belongs_to :step
  belongs_to :project

  validates :url, url: true
  validates :url, :presence=> true
  validates :embed_code, :presence => true
  validate :url_is_from_approved_site
  ...
  end

图片.rb:

class Image < ActiveRecord::Base

  attr_accessible :project_id, :step_id, :imagePath, :caption, :position, :saved

  belongs_to :step
  belongs_to :project

  mount_uploader :imagePath, ImagePathUploader

  before_create :default_name

  validates :imagePath, :presence => true

  def default_name
    self.imagePath ||= File.basename(imagePath.filename, '.*').titleize if imagePath
  end

  def image=(val)
    if !val.is_a?(String) && valid?
      image_will_change!
      super
    end
  end

end
4

2 回答 2

1

你已经设置:imagePath了,但它应该是:imagepath。此外,您可以使用create!方法而不是new后跟save!

Image.create!(:step_id=>@video.step_id, :imagepath=>@video.thumbnail_url, :project_id=>@video.project_id, :saved=> true, :position=>position)
于 2013-07-05T18:48:41.257 回答
0

The error ended up being that you can't manually override Carrierwave attributes. Instead, I need to use :remote_image_path_url to set the :image_path attribute:

Manually Setting Image Path for Carrierwave Uploader

This uploads the image from the url via carrierwave directly (rather than pointing to the URL for the image).

于 2013-07-05T20:25:01.733 回答