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 回答