Paperclip保存在控制台中,而不是视图中,没有错误

时间:2011-03-09 16:39:00

标签: ruby-on-rails ruby-on-rails-3 forms model paperclip

我有以下型号:

class Folder < ActiveRecord::Base

  # RELATIONSHIPS
  belongs_to :collection
  belongs_to :parent, :class_name => 'Folder'
  has_many :subfolders, :class_name => 'Folder', :foreign_key => :parent_id
  has_many :photos

  has_attached_file :image,
                    :styles => { :sixth => "145x109#", :eighth => "106x80#", :tenth => "87x65#" },
                    :storage => :s3,
                    :s3_credentials => "#{Rails.root}/config/s3.yml",
                    :path => "collections/:id/:style.:extension",
                    :default_url => '/images/no_folder_image_:style.png'

  ...
end

当我在控制台中执行此操作时...

f = Folder.first
f.image = "foo.jpg"
f.save!

...然后图像得到保存并完美运行。

但是,当我尝试使用表单创建或更新图像时,我没有错误,但它不起作用。

以下是日志示例:

Started POST "/collections/community-collection/folders/11" for 127.0.0.1 at 2011-03-09 10:31:05 -0600
  Processing by Collections::FoldersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"RpY1Ov1oMsA7JhvpNZpvuoJfNaRV8BAjiCbHYkUQSus=", "folder"=>{"name"=>"Test With Image", "parent_id"=>"1", "image"=>"Snowy Chicago.jpg", "description"=>""}, "commit"=>"Update Folder", "collection_id"=>"community-collection", "id"=>"11"}
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
  SQL (0.2ms)  SELECT sluggable_id FROM slugs WHERE ((slugs.sluggable_type = 'Collection' AND slugs.name = 'community-collection' AND slugs.sequence = 1))
  Collection Load (0.1ms)  SELECT "collections".* FROM "collections" WHERE "collections"."id" = 1 LIMIT 1
  Role Load (0.2ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles".id = "roles_users".role_id WHERE "roles"."name" = 'admin' AND ("roles_users".user_id = 2 ) LIMIT 1
  Role Load (0.3ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles".id = "roles_users".role_id WHERE "roles"."name" = 'curator' AND ("roles_users".user_id = 2 ) LIMIT 1
  Role Load (0.3ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles".id = "roles_users".role_id WHERE "roles"."name" = 'member' AND ("roles_users".user_id = 2 ) LIMIT 1
  User Load (0.9ms)  SELECT "users".*, "users"."id" FROM "users" INNER JOIN "assignments" ON "users".id = "assignments".user_id WHERE "users"."id" = 2 AND (("assignments".collection_id = 1)) LIMIT 1
  Folder Load (0.6ms)  SELECT "folders".* FROM "folders" WHERE "folders"."id" = 11 AND ("folders".collection_id = 1) ORDER BY "folders"."display_order" ASC LIMIT 1
  CACHE (0.0ms)  SELECT "collections".* FROM "collections" WHERE "collections"."id" = 1 LIMIT 1
[paperclip] Saving attachments.
  Slug Load (0.3ms)  SELECT "slugs".* FROM "slugs" WHERE ("slugs".sluggable_id = 1 AND "slugs".sluggable_type = 'Collection') ORDER BY id DESC LIMIT 1
Redirected to http://localhost:3000/collections/community-collection/folders
Completed 302 Found in 375ms

请注意,它显示[paperclip] Saving attachments.

以下是控制器操作:

# this before_filter loads the collection folders belong to:
def load_collection
  @collection = Collection.find(params[:collection_id])
end

def create
  if @collection.folders.create(params[:folder])
    redirect_to collection_folders_path(@collection)
  else
     render 'new'
  end
end

def update
  @folder = @collection.folders.find(params[:id])
  if @folder.update_attributes(params[:folder])
    redirect_to collection_folders_path(@collection)
  else
    render 'edit'
  end
end

以下是表格:

= simple_form_for [@collection, @folder], :html => { :class => 'full' } do |f|
  = f.input :name
  = f.association :parent
  = f.input :image, :hint => 'Choose an image or icon to represent this folder.'
  = f.input :description
  = f.submit

此表单按预期工作,保存所有其他属性,但由于某些原因,paperclip不起作用。值得一提的是,simple_form不是原因,我在其他模型上有其他几乎相同的形式,其中回形针附件都可以完美运行。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:3)

您需要将multipart属性设置为true

simple_form_for [@collection, @folder], :html => { :class => 'full', :multipart => true } do |f|

<强>更新

如果您使用的是Rails 3.1,则不需要将multipart属性设置为true,因为它是自动处理的。