2

User モデルのネストされたリソース Picture があります。「remote_file_url」で写真をアップロードしようとすると、写真はcarrierwave tmp ディレクトリにアップロードされますが、実際のディレクトリには移動されません。それは「すべき」検証の問題です。そうではありません。コンソールで問題なく動作します:

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

my ユーザーを画像で更新するためのリクエストのパラメーター:

 "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
4

2 に答える 2

1

attr_accessible が原因です。マウントされたアップローダのインスタンス変数名を attr_accessible にすると、Ruby がアクセサを作成します... これは CarrierWave のセッターに干渉します。

マウントされたアップローダー変数名を attr_accessible にしないでください。

于 2012-10-01T00:24:00.847 に答える
0

remote_image_url を設定すると、画像がモデル自体と一緒に保存されないという同様の問題がありました。

問題は、remote_image_url が設定された後に画像が保存されないことでした。

これはself.taker = 'Andrew'、Picture.taker の「Andrew」をデータベースに自動的に保存する User モデルからデリゲートされたメソッドを呼び出したためです。それを避けて、'self.picture.taker = 'Andrew' を使用して、画像が永続化されないようにする必要がありました。次に、user.save を呼び出すと、関連する画像が保存され、サムネイル画像が正しく作成されます。

これは特定のケースですが、ポイントは、remote_image_url が設定された後に画像オブジェクトが実際に保存されているかどうかを確認することです。

于 2012-02-22T09:03:32.733 に答える