Friday, March 28, 2008

Rails a upload plików z klasą polimorficzną (Attachment_fu)

Polecam zainstalowanie plug-inu Attachment_fu


script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu


Następnie tworzymy model np. File /lub plik - tworząc odpowiednie inflections/, czyli


scrip/generate model Plik


w stworzona migracja powinna wyglądać następująco:


class CreatePliki < ActiveRecord::Migration
def self.up
create_table :pliki do |t|
t.column :filename, :string
t.column :size, :integer
t.column :content_type, :string
t.column :thumbnail, :string
t.column :parent_id, :integer
t.column :height, :integer
t.column :width, :integer

# Obsługa polimorfizmu
t.column :zasob_id, :integer
t.column :zasob_type, :string

t.timestamps
end
end

def self.down
drop_table :pliki
end
end


A nasz plik modelu powinien zawierać następujący wpis:


class Plik < ActiveRecord::Base
belongs_to :zasob, :polymorphic => true # Polimorfizm

has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 2.megabytes,
:resize_to => '300x300>',
:size => 0..2.megabyte,
:thumbnails => {
:small => '50x50>',
:medium => '100x100>',
:big => '200x200>'
},
:processor => :MiniMagick;

validates_as_attachment
end


Zdefiniowane zostały tutaj opcje zapisu zdjęcia uwzględniające tworzenie miniaturek za pomocą biblioteki MiniMagic. Aby ją zainstalować powinniśmy wpisać w konsoli:

sudo gem install mini_magick

Teraz wiążemy nasze zdjęcie z jakąś istniejącą klasą modelu. W naszym przypadku niech będzie nią klasa produkt:


class Produkt < ActiveRecord::Base

has_and_belongs_to_many :kategorie
belongs_to :producent

has_one :plik, :as => :zasob #,:class_name => 'Plik'

validates_presence_of :nazwa, :message => "nie może być pusta"
validates_presence_of :cena, :message => "nie może być pusta"

validates_length_of :nazwa, :minimum=>2, :message=> "musi składać się przynajmniej z dwóch znaków"
validates_numericality_of :cena, :greater_than_or_equal_to => 0, :message => "nie może być ujemna"

end


W następnej kolejności musimy uzupełnić widoki o możliwość dodawania oraz wyświetlania naszego obrazka. W formularzu dla akcji new oraz edit powinniśmy mieć następujący wpis


<% form_for(@produkt, :html => { :multipart => true }) do |f| %>
....
<%= file_field :plik, :uploaded_data %>
....
<% end %>


Musimy również uzupełnić nasz kotroler o zapis załączonego pliku. W akcji update oraz create należy dodać następujący kod:


unless params[:plik].blank?
@produkt.plik = Plik.new(params[:plik])
p.save
end


Wyświetlić nasz załączony plik możemy w następujący sposób:


<%= image_tag(@produkt.plik.public_filename(:big)) if @produkt.plik%>


gdzie :big to nazwa thumbnailsa podana w modelu. Jeżeli nie podamy tego parametru wyświetlony będzie oryginalny obrazek.

Przedstawiłem podstawowe kroki niezbędne do dodania możliwości przechowywania plików związanych z naszymi klasami modelu. Po więcej informacji odsyłam do:

http://www.railslodge.com/plugins/108-attachment-fu
http://clarkware.com/cgi/blosxom/2007/02/24
http://blog.defv.be/2007/12/19/attachment_fu-with-polymorphic-association
http://khamsouk.souvanlasy.com/2007/5/1/ajax-file-uploads-in-rails-using-attachment_fu-and-responds_to_parent

8 comments:

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Nice brief and this enter helped me alot in my college assignement. Thanks you on your information.

black lesbian sex stories said...

We have to stop them from fucking. When he removed his tongue,she said, Lets go into the bedroom.
femalelesbian masturbation stories
hot wet sex stories
bdsm tortureincest stories
brother sister incest sex stories
first time lesban stories
We have to stop them from fucking. When he removed his tongue,she said, Lets go into the bedroom.

Anonymous said...

That being said, it can be seen that Apple and Samsung continue to grab market share from other OEMs.
When it comes to connectivity, the T-Mobile G1 packs a
real punch. However, they are less likely to say these tools
helped them save money at a rate of 44 percent to 49 percent.


My site :: samsung galaxy s4

Anonymous said...

Look at the user remarks in places like Amazon and B&H photo.
Obviously, there may be some changes in the images produced by
the pre and production run 5DIIIs. According to Chung, the
Nikon has the edge for detail and for the addition of a crop mode while canon has the advantage for lack of moire and high ISO performance.


My web-site: canon mark 5d iii

Anonymous said...

The apple tv
is much cheaper now, at only $99, a far cry from what it used to sell for back in 2007 when it was more than $200.
The built-in web browser of Google TV separates it from media streaming devices
like Apple TV. In this day and age it is impossible to stay on top of all the latest pieces of technology.

Anonymous said...

Black - Berry Play - Book by RIM is at price of Rs 32000 for 16GB model.
It has all the extra features that the i - Pad has too, just fewer apps because it's fairly new compared to the i - Pad. Apple products include the Iphones, Ipads and Ipods all at competitive prices.

my homepage: blackberry playbook

Anonymous said...

To get these private channel codes you can go to streamfreetv.
All 3 of the devices take care of high-definition streaming video, although the XD is the most reasonably priced with 1080p capability.
Apple TV and Roku are two devices you can use to stream High Definition (HD) Video, movies, TV
Shows, photos, and more to your HD TV wirelessly and
connect to your wireless environment and connect to the internet.


Here is my homepage: roku 2 xs