2013-07-05 49 views
0

我有兩個模型:視頻和圖像。Rails創建新的活動記錄返回false

當我創建新視頻時,我想保存視頻縮略圖圖像。我試圖用這個在我的視頻控制器「創造」行動以下行:

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

這可能是一個驗證問題;或者使用'save!'(如果驗證失敗,這將拋出一個異常並帶有解釋) – Baldrick

+1

運行newImage.errors.full_messages,看看你得到了什麼。 –

+0

我收到錯誤「驗證失敗:Imagepath不能爲空」但在我的日誌消息中,它顯示我沒有發送空白imagePath,因此不明白爲什麼我仍然收到此錯誤。 – scientiffic

回答

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

不,我的屬性被稱爲imagePath,而不是圖像路徑。出於某種原因,它沒有正確設置圖像路徑。 – scientiffic

+1

您的屬性應該被稱爲imagepath或image_path。您不應該像使用大寫字母那樣使用屬性名稱。我相信這會導致與驗證不一致。 –

+0

好吧,我經歷了(非常長的)重構我的代碼的過程,使其具有屬性image_path而不是imagePath。即使如此,當創建活動記錄時,我得到的image_path被接收爲「無」的相同確切錯誤。你還有其他建議嗎? – scientiffic

相關問題