我有一個用戶模型的嵌套資源圖片。如果我嘗試上傳帶有「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
視圖是什麼樣子?和什麼是控制器的代碼? – 2011-06-15 23:25:08
我更新了問題... – fluxsaas 2011-06-16 00:01:07
什麼是視圖的實際erb? – 2011-06-16 00:38:13