2011-06-15 50 views
2

我有一個用戶模型的嵌套資源圖片。如果我嘗試上傳帶有「remote_file_url」的圖片,它會上載圖片在載波波形目錄中,但並未將它們移動到實際目錄。那「應該」是一個驗證問題。但事實並非如此。它在控制檯中工作得很好:上傳文件從遠程位置和刪除不起作用在載波

p = Picture.find(360) 
p.remote_file_url = "http://example.com/somepic.jpg" 
p.save! 

我的請求參數與圖片更新用戶:

"user"=>{"pictures_attributes"=>{"0"=>{"remote_file_url"=>"http://example.com/somepic.jpg", "id"=>"359"} 

,如果我上傳未經remote_file_url(只需輸入文件字段),它作品:

"user"=>{"pictures_attributes"=>{"0"=> { "file"=>#<ActionDispatch::Http>, "remote_file_url"=>"", "id"=>"359"} 

當我使用「remove_file」功能時會發生同樣的問題。在控制檯中工作得很好,但不適用於視圖/控制器更新。

===================

更新

控制器:

def update 
    @user = current_user 
    if @user.update_attributes!(params[:user]) 
     respond_to do |format| 
     format.js { render :json => @user.to_json(:methods => [:pictures]) } 
     format.html {redirect_to :back, :notice => "Successfully updated user."} 
     end 
    else 
     render :action => 'edit' 
    end 
    end 

圖片型號:

class Picture < ActiveRecord::Base 
    attr_accessible :file, :file_cache, :remove_file, :position, :remote_file_url 
    mount_uploader :file, PictureUploader 
    belongs_to :user 
end 

用戶模型

# encoding: UTF-8 
class User < ActiveRecord::Base 
    attr_accessible :remote_file_url, :beta_token, :wants_gender, :gender, :radius, :email, :password, :password_confirmation, :remember_me, :region, :latitude, :longitude, :gmaps, :pictures_attributes 
    has_many :pictures, :dependent => :destroy 
    accepts_nested_attributes_for :pictures, :allow_destroy => true 
end 

非常基本的東西....我刪除了色器件定義爲用戶模型....

= simple_form_for @user, :url => profile_path(@user) do |f| 
    %ul.profiles.users 
    - @user.pictures.each_with_index do |picture, i| 
     %li.user 
     .fields 
     = f.simple_fields_for :pictures, picture do |picture_from| 
      - if picture.file.present? 
      = picture_from.input :_destroy, :as => :boolean 
      = picture_from.input :position 
      = picture_from.input :file 
      = picture_from.input :remote_file_url, :as => :hidden 
      = picture_from.input :file_cache 
+0

視圖是什麼樣子?和什麼是控制器的代碼? – 2011-06-15 23:25:08

+0

我更新了問題... – fluxsaas 2011-06-16 00:01:07

+0

什麼是視圖的實際erb? – 2011-06-16 00:38:13

回答

0

我曾在一個類似的問題時,我設置了remote_image_url圖像不救以及模型本身。

問題是在remote_image_url設置後,圖片沒有保存。

這是因爲我從用戶模型中調用了一個委託方法self.taker = 'Andrew',它將picture.taker中的'Andrew'自動保存到數據庫中。我必須避免這種情況,並使用'self.picture.taker ='安德魯'來避免圖片被持續。然後,當我呼叫user.save它會保存相關的圖片,並正確創建縮略圖。

這種情況是特定的,但重點是檢查您的圖片對象是否已設置remote_image_url後實際保存。

1

這是因爲attr_accessible。如果您將裝入的上傳器的實例變量名稱設置爲attr_accessible,則Ruby將爲您創建訪問器......這會干擾CarrierWave的setter。

請勿使任何裝入的上傳器變量名稱attr_accessible。