创建新活动记录的Rails返回false

时间:2013-07-05 18:14:41

标签: ruby-on-rails activerecord

我有两种模式:视频和图像。

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

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)

Video.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

Image.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

2 个答案:

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

答案 1 :(得分:0)

错误最终导致您无法手动覆盖Carrierwave属性。相反,我需要使用:remote_image_path_url来设置:image_path属性:

Manually Setting Image Path for Carrierwave Uploader

这会直接通过carrierwave从网址上传图片(而不是指向图片的网址)。