Używam w mojej aplikacji attachment_fu wraz z attribute_fu przy uploadzie zdjęć do artykułów. Wszystko przy uplaodzie jest ok mogę dodać kilka zdjęć jednocześnie ale przy edycji gdy chcę dodać np kolejne zdjęcia pojawia sie problem bo system dodaje te które chcę dodać dodatkowo a te stare usuwa i nie wiem gdzie istnieje problem:
model Article:
class Article < ActiveRecord::Base
has_many :attachments, :as => :attachable, :dependent => :destroy, :attributes => true,
:discard_if => proc { |attachment| attachment.size.nil? }
end
model Attachment
class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
has_attachment :storage => :file_system,
:path_prefix => 'public/files',
:resize_to => '640x480',
:thumbnails => { :thumb => '120x80', :window => '80>', :tiny => '50>'},
:max_size => 5.megabytes,
:content_type => :image,
:processor => 'Rmagick'
end
Article Controller:
[code]class ArticlesController < ApplicationController
def new
@article = Article.new
end
def create
@article = Article.new(params[:article])
@logged_in_user.articles << @article
if @article.save
respond_to do |wants|
wants.html { redirect_to admin_articles_url }
wants.xml { render :xml => @articles.to_xml }
end
else
render :action => :new
end
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
respond_to do |wants|
wants.html { redirect_to admin_articles_url }
wants.xml { render :xml => @article.to_xml }
end
else
redirect_to admin_articles_url
flash[:error] = “Do not save this article!”
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
respond_to do |wants|
wants.html { redirect_to admin_articles_url }
wants.xml { render :nothing => true }
end
end
end[/code]
Widok edit.html
[code]
Edycja Artykułu
<% form_for :article,
:url => article_url(@article),
:html => {:multipart => true, :method => :put} do |f| -%>
-
<% if @article.attachments.size > 0 -%>
<%= render :partial=> "uploaded_attachment", :collection => @article.attachments.find(:all) %>
<% end -%>
Nowy Artykuł
<% form_for :article, :url => articles_url, :html => { :multipart => true, :method => :post } do |f| -%>
Parameters: {“article”=>{“attachment_attributes”=>{“new”=>{"-1"=>{“uploaded_data”=>#<File:C:/DOCUME~1/JAREKM~1/USTAWI~1/Temp/RackMultipart5216-0>}, “-2”=>{“uploaded_data”=>#<File:C:/DOCUME~1/JAREKM~1/USTAWI~1/Temp/RackMultipart5216-1>}, “-3”=>{“uploaded_data”=>#<File:C:/DOCUME~1/JAREKM~1/USTAWI~1/Temp/RackMultipart5216-2>}}}, “body”=>“
das
”, “title”=>“dasd”, “published”=>“1”, “category_id”=>“1”, “synopsis”=>“dasd
”}, “commit”=>“Zapisz”, “id”=>“4”}User Columns (0.0ms) SHOW FIELDS FROM users
User Load (0.0ms) SELECT * FROM users
WHERE (users
.id
= 1)
Role Columns (0.0ms) SHOW FIELDS FROM roles
Role Load (0.0ms) SELECT roles
.* FROM roles
INNER JOIN roles_users
ON roles
.id = roles_users
.role_id WHERE (roles
.name
= ‘Editor’) AND (roles_users
.user_id = 1 ) LIMIT 1
Article Columns (0.0ms) SHOW FIELDS FROM articles
Article Load (0.0ms) SELECT * FROM articles
WHERE (articles
.id
= 4)
Attachment Columns (0.0ms) SHOW FIELDS FROM attachments
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.attachable_id = 4 AND attachments
.attachable_type = ‘Article’)
SQL (0.0ms) BEGIN
SQL (0.0ms) COMMIT
SQL (0.0ms) BEGIN
Article Update (0.0ms) UPDATE articles
SET published_at
= ‘2010-09-17 08:32:44’, updated_at
= ‘2010-09-17 08:32:44’ WHERE id
= 4
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘tiny’ AND attachments
.parent_id
= 105) LIMIT 1
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘window’ AND attachments
.parent_id
= 105) LIMIT 1
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘thumb’ AND attachments
.parent_id
= 105) LIMIT 1
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘tiny’ AND attachments
.parent_id
= 109) LIMIT 1
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘window’ AND attachments
.parent_id
= 109) LIMIT 1
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘thumb’ AND attachments
.parent_id
= 109) LIMIT 1
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘tiny’ AND attachments
.parent_id
= 113) LIMIT 1
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘window’ AND attachments
.parent_id
= 113) LIMIT 1
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘thumb’ AND attachments
.parent_id
= 113) LIMIT 1
Attachment Create (0.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 2450619, NULL, ‘2010-09-17 08:32:45’, 480, ‘IMG_5739.JPG’, ‘Article’, NULL, 4, NULL, NULL, 640, ‘2010-09-17 08:32:45’)
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘tiny’ AND attachments
.parent_id
= 117) LIMIT 1
Attachment Create (0.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 192320, ‘tiny’, ‘2010-09-17 08:32:45’, 38, ‘IMG_5739_tiny.JPG’, NULL, 117, NULL, NULL, NULL, 50, ‘2010-09-17 08:32:45’)
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘window’ AND attachments
.parent_id
= 117) LIMIT 1
Attachment Create (0.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 192320, ‘window’, ‘2010-09-17 08:32:45’, 60, ‘IMG_5739_window.JPG’, NULL, 117, NULL, NULL, NULL, 80, ‘2010-09-17 08:32:45’)
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘thumb’ AND attachments
.parent_id
= 117) LIMIT 1
Attachment Create (16.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 192320, ‘thumb’, ‘2010-09-17 08:32:45’, 80, ‘IMG_5739_thumb.JPG’, NULL, 117, NULL, NULL, NULL, 107, ‘2010-09-17 08:32:45’)
Attachment Create (0.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 2450619, NULL, ‘2010-09-17 08:32:46’, 480, ‘IMG_5739.JPG’, ‘Article’, NULL, 4, NULL, NULL, 640, ‘2010-09-17 08:32:46’)
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘tiny’ AND attachments
.parent_id
= 121) LIMIT 1
Attachment Create (0.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 192320, ‘tiny’, ‘2010-09-17 08:32:46’, 38, ‘IMG_5739_tiny.JPG’, NULL, 121, NULL, NULL, NULL, 50, ‘2010-09-17 08:32:46’)
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘window’ AND attachments
.parent_id
= 121) LIMIT 1
Attachment Create (0.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 192320, ‘window’, ‘2010-09-17 08:32:46’, 60, ‘IMG_5739_window.JPG’, NULL, 121, NULL, NULL, NULL, 80, ‘2010-09-17 08:32:46’)
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘thumb’ AND attachments
.parent_id
= 121) LIMIT 1
Attachment Create (0.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 192320, ‘thumb’, ‘2010-09-17 08:32:46’, 80, ‘IMG_5739_thumb.JPG’, NULL, 121, NULL, NULL, NULL, 107, ‘2010-09-17 08:32:46’)
Attachment Create (0.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 2351949, NULL, ‘2010-09-17 08:32:46’, 480, ‘IMG_5736.JPG’, ‘Article’, NULL, 4, NULL, NULL, 640, ‘2010-09-17 08:32:46’)
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘tiny’ AND attachments
.parent_id
= 125) LIMIT 1
Attachment Create (0.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 160061, ‘tiny’, ‘2010-09-17 08:32:46’, 38, ‘IMG_5736_tiny.JPG’, NULL, 125, NULL, NULL, NULL, 50, ‘2010-09-17 08:32:46’)
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘window’ AND attachments
.parent_id
= 125) LIMIT 1
Attachment Create (0.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 160061, ‘window’, ‘2010-09-17 08:32:46’, 60, ‘IMG_5736_window.JPG’, NULL, 125, NULL, NULL, NULL, 80, ‘2010-09-17 08:32:46’)
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.thumbnail
= ‘thumb’ AND attachments
.parent_id
= 125) LIMIT 1
Attachment Create (0.0ms) INSERT INTO attachments
(content_type
, size
, thumbnail
, updated_at
, height
, filename
, attachable_type
, parent_id
, attachable_id
, position
, path
, width
, created_at
) VALUES(‘image/pjpeg’, 160061, ‘thumb’, ‘2010-09-17 08:32:46’, 80, ‘IMG_5736_thumb.JPG’, NULL, 125, NULL, NULL, NULL, 107, ‘2010-09-17 08:32:46’)
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.parent_id = 105)
Attachment Destroy (0.0ms) DELETE FROM attachments
WHERE id
= 106
Attachment Destroy (0.0ms) DELETE FROM attachments
WHERE id
= 107
Attachment Destroy (0.0ms) DELETE FROM attachments
WHERE id
= 108
Attachment Destroy (0.0ms) DELETE FROM attachments
WHERE id
= 105
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.parent_id = 109)
Attachment Destroy (16.0ms) DELETE FROM attachments
WHERE id
= 110
Attachment Destroy (0.0ms) DELETE FROM attachments
WHERE id
= 111
Attachment Destroy (0.0ms) DELETE FROM attachments
WHERE id
= 112
Attachment Destroy (0.0ms) DELETE FROM attachments
WHERE id
= 109
Attachment Load (0.0ms) SELECT * FROM attachments
WHERE (attachments
.parent_id = 113)
Attachment Destroy (0.0ms) DELETE FROM attachments
WHERE id
= 114
Attachment Destroy (0.0ms) DELETE FROM attachments
WHERE id
= 115
Attachment Destroy (0.0ms) DELETE FROM attachments
WHERE id
= 116
Attachment Destroy (0.0ms) DELETE FROM attachments
WHERE id
= 113
SQL (0.0ms) COMMIT
Redirected to http://localhost:3000/articles/admin
Completed in 5891ms (DB: 32) | 302 Found [http://localhost/articles/4][/code]