0

Rails 3.2とPaperclipを使用して、HTML5を使用して複数のファイル(写真)を一度にアップロードしますmultipart。これが私のフォームです:

# shops/_form.html.erb
<%= form_for @shop, :url => { :action => action, :type => type }, :html => { :multipart => true } do |f| %>
  <%= f.text_field :name %>
  <%= f.file_field :shop_photos_data, :multiple => true, :name => "shop[photos_attributes][][data]" %>
<% end %>

動作し、更新/作成すると次の結果が得られます。

{"utf8"=>"✓", 
"authenticity_token"=>"9jXvIwcllct7UyUfo6cvhEucQf2u3SY50SuaCLtFO4c=", 
"shop"=>{
  "name"=>"First shop", 
  "photos_attributes"=>{"0"=>{
    "image"=>[
      #<ActionDispatch::Http::UploadedFile:0x00000104b78978 
        @original_filename="first_test_image.jpg", 
        @content_type="image/jpeg", 
        @headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"first_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n", 
        @tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-vz78ee>>, 
      #<ActionDispatch::Http::UploadedFile:0x00000104b78950 
        @original_filename="second_test_image.jpg", 
        @content_type="image/jpeg", 
        @headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"second_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n", 
        @tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-1jzhhyg>>
      ]
    }
  }
}, "commit"=>"Save", "action"=>"create", "controller"=>"shops"}

それは機能し、に行きますが、にshops_controller.rbは行きませんphotos_controller.rb

コードの他の部分は次のとおりです。

# photo.rb
class Photo < ActiveRecord::Base
  belongs_to :attachable, :polymorphic => true, :counter_cache => true
  belongs_to :user, :counter_cache => true
  attr_accessible :data, :attachable_id, :attachable_type, :user_id
end

# shop.rb
class Shop < ActiveRecord::Base  
  attr_protected :photos_count
  has_many :photos, :as => :attachable, :dependent => :destroy
  accepts_nested_attributes_for :photos, :allow_destroy => true
end

# photos_controller.rb
class PhotosController < ApplicationController
end

# shops_controller.rb
class ShopsController < ApplicationController
  before_filter :require_user, :only => [:new, :edit, :update, :create]

  ...

  def update
    @shop = Shop.find(params[:id])
    if @shop.update_attributes(params[:shop])
      flash[:notice] = 'Successfully updated.'
      redirect_to shop_path(@shop)
    else
      render :action => :edit
    end
  end
end

モデルにuser_idフィールドがあります。Photo現在、は新しいレコードuser_idごとに保存されていません。をファイルアップロード配列に含めるにはPhotoどうすればよいですか?セキュリティが公開されるため、フォームで実行したくありません。shops_controller.rbuser_id

ありがとう。

4

2 に答える 2

0

これを入れてshops_controller.rb

def update
  @shop = Shop.find(params[:id])

  photos = params[:shop][:photos_attributes]

  if !photos.blank?
    photos.each do |photo|
      photo.merge!(:user_id => current_user.id)
    end
  end

  if @shop.update_attributes(params[:shop])
    flash[:notice] = 'Successfully updated.'
    redirect_to shop_path(@shop)
  else
    render :action => :edit
  end
end
于 2012-12-02T15:08:26.417 に答える
0

フィールドをフォームに追加するには、次のような非表示のフィールドを使用します。

<%= hidden_field_tag "user", @user.id %>

次に、コントローラーで次のようにアクセスできます。

params[:user]
于 2012-12-02T16:03:21.980 に答える