12

私はいくつかの例を探していましたが、不足しています:

私が取り組んでいるプロジェクトに JQuery-File-Upload を実装しようとしていますが、ネストされた属性で動作させる方法について迷っています。

簡単な概要:

2 モデル:

Comment [has_many :attachments]
Attachment [belongs_to :comment]

コメントは accept_nested_attributes_for を受け入れ:attachmentsます。また、私はDragonflyを使用しています。

JQuery-File-Upload サイトで Rails 3 ガイドを確認しましたが、Rails 3 は単一のモデルであると想定されているため、すべてフォームを中心に構築されています。誰かが実装の例を持っていますか、それとも私がまだ遭遇していない既存のチュートリアルがありますか?

誰かが同様の問題を抱えていると確信しています... JQuery-File-Upload は適切なツールですか、それとも何か他のものを見る必要がありますか?

4

3 に答える 3

6

ストーンの答えと同様に、ここに私の答えを入れたかっただけです。私はこれを機能させるのに2日近く費やしました(Stoneは正しかった、それはPITAでした!)ので、私の解決策が誰かを助けることを願っています. ストーンとは少し違うだけでした。

私のアプリにはFeatures、(コミック、パズル、テキスト コラムなど) とFeatureAssets(個々のコミック パネル/カラー バージョン、特定のクロスワードの Q&A ファイルなど) があります。FeatureAssetsは 1 つの のみに関連しているためFeature、モデルをネストしました (アップロード フォームで確認できます)。

params[:feature_asset]私にとっての最大の問題は、サーバーに送信されていたものが、私が慣れ親しんでいたものではなく、実際にはアップローダfileのオブジェクトの配列であることに気付いたことでした。各ファイルを繰り返し処理し、そこから FeatureAsset を作成することを少しいじった後、それは魔法のように機能しました!

うまくいけば、私はこれを明確に翻訳します。十分な情報を提供しないよりも、少し多すぎる情報を提供したいと思います。他の誰かのコードを解釈しているときに、少し余分なコンテキストが問題になることはありません。

機能.rb

class Feature < ActiveRecord::Base
  belongs_to :user
  has_many :feature_assets

  attr_accessible :name, :description, :user_id, :image

  accepts_nested_attributes_for :feature_assets, :allow_destroy => true

  validates :name,    :presence => true
  validates :user_id, :presence => true

  mount_uploader :image, FeatureImageUploader
end

feature_asset.rb

  belongs_to :user
  belongs_to :feature

  attr_accessible :user_id, :feature_id, :file, :file_cache

  validates :user_id,     :presence => true
  validates :feature_id,  :presence => true
  validates :file,        :presence => true

  mount_uploader :file, FeatureAssetContentUploader

  # grabs useful file attributes & sends them as JSON to the jQuery file uploader
  def to_jq_upload
    {
      "file" => file,
      "file_name" => 'asdf',
      "url" => file.url,
      "delete_url" => id,
      "delete_type" => "DELETE"
    }
  end

feature_assets_controller.rb

  def create
    @feature = Feature.find(params[:feature_id])

    params[:feature_asset]['file'].each do |f|
      @feature_asset = FeatureAsset.create!(:file => f, :feature_id => @feature.id, :user_id => current_user.id)
    end

    redirect_to @feature
  end

おそらくそれほど役立つわけではありませんが、私の feature_asset_uploader.rb は以下にあります。かなり削ぎ落とされています。

class FeatureAssetContentUploader < CarrierWave::Uploader::Base

  storage :file

end

features _form.html.erb (Stoneのものに似ていますが、完全ではありません)

<%= form_for [@feature, @feature_asset], :html => { :multipart => true  } do |f| %>
  <div class="row" id="fileupload">
    <div class=" fileupload-buttonbar">
      <div class="progressbar fileupload-progressbar nofade"><div style="width:0%;"></div></div>
      <span class="btn btn-primary fileinput-button">
        <i class="icon-plus"></i>
        <span><%= t('feature_assets.add_files') %>...</span>
        <%= hidden_field_tag :feature_id, @feature.id %>
        <%= hidden_field_tag :user_id, current_user.id %>
        <%= f.file_field :file, :multiple => true %>
      </span>
      <button type="submit" class="btn btn-success">Start Upload</button>
      <button type="reset" class="btn btn-warning">Cancel Upload</button>
      <button type="button" class="btn btn-danger">Delete Files</button>
    </div>
  </div>

エラー処理や必要な機能はありませんが、それはベアボーン バージョンです。

うまくいけば、それが誰かを助けるでしょう。ご不明な点がございましたら、お気軽にお問い合わせください。

カイル

于 2013-06-18T21:22:03.933 に答える
1

私はCarrierwaveで実行している同様のセットアップを持っています。これが私が持っているものです。プロジェクトのネストされたリソースとして画像を使用しています。

Project.rb:

has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images, :allow_destroy => true

Image.rb:

 include Rails.application.routes.url_helpers
  mount_uploader :image, ImageUploader

  belongs_to :project

  #one convenient method to pass jq_upload the necessary information
  def to_jq_upload
  {
    "name" => read_attribute(:image),
    "size" => image.size,
    "url" => image.url,
    "thumbnail_url" => image.thumb.url,
    "delete_url" => image_path(:id => id),
    "delete_type" => "DELETE" 
   }
  end

Images_controller.rb:

 def create
    @image = Image.new(params[:image])
    @image.project_id = params[:project_id]
    @project = Project.find(params[:project_id])
    @image.position = @project.images.count + 1
    if @image.save
      render :json => [ @image.to_jq_upload ].to_json
    else
      render :json => [ @image.to_jq_upload.merge({ :error => "custom_failure" }) ].to_json
    end
  end

注意してください、これは*!@ ^%!働くために。

更新:projects / _form.html.erb

<div id="fileupload" class="image_add">
    <%= form_for Image.new, :html => {:multipart => true} do |f| %>
        <div class="fileupload-buttonbar">
            <label class="fileinput-button">
                <span>Add files...</span>
                <%= hidden_field_tag :project_id, @project.id %>
                <%= f.file_field :image %>
            </label>
            <button type="submit" class="start">Start Upload</button>
            <button type="reset" class="cancel">Cancel Upload</button>
            <button type="button" class="delete">Delete Files</button>
        </div>
    <% end %>
    <div class="fileupload-content">
        <div class="dropzone-container">
            <div class="dropzone">Drop Image Files Here</div>
        </div>
        <table class="files"></table>
    </div>
</div>
于 2012-05-18T22:29:35.627 に答える