开发者

Uploading files from a remote location and removing does not work in carrierwave

i have a nested ressource Picture for a User model. if i try to upload a picture with a "remote_file_url" it uploads the pictures in the carrierwave tmp directory but did not moves them to the actual dir. that "should" be a validation problem. but it is not. it does work in t开发者_JAVA百科he console just fine:

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

my Parameters for the request for updating the user with picture:

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

if i upload without the remote_file_url (just the input file field), it works:

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

the same problem occurs when i use the "remove_file" feature. works just fine in the console, but does not work with the view/controller update.

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

Update

controller:

  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

Picture Model:

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

User Model

# 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

pretty basic stuff.... i removed the devise definitions for the user model....

= 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


It's because of attr_accessible. If you make the mounted uploader's instance variable name attr_accessible, Ruby is going to create accessors for you... which will interfere with CarrierWave's setter.

Don't make any mounted uploader variable names attr_accessible.


I had a similar problem in that when I set the remote_image_url the image doesn't save along with the model itself.

The problem was that the picture was not saving after the remote_image_url was set.

This was cause because I called a delegated method self.taker = 'Andrew' from User model, which automatically saves 'Andrew' in the picture.taker onto the database. I had to avoid that and use 'self.picture.taker = 'Andrew' to avoid the picture from being persisted. Then when I call user.save it will save the associated picture and have thumbnail images created correctly.

This case is specific, but the point is that check if your picture object has actually been saved after the remote_image_url has been set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜