step:1
install attachment_fu plugin
rails_app>script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/
step:2
install minimagick gem
rails_app>gem install minimagick(which is better in memory occuping from rest of it)
step:3
create a controller medias_controller.rb
def index
@medias = Media.find(:all)
end
def new
@media=Media.new
end
def create
@media = Media.new(params[:media])
if @media.save
flash[:notice] = "image successfully uploaded"
redirect_to :action => 'show'
else
logger.debug"%%%%%%%%%%%%% inside else"
flash[:notice]="image not uploaded"
redirect_to :action =>'show'
end
end
def show
@media = Media.find(params[:id])
end
def delete
@media = Media.find(params[:id])
@media.destroy
redirect_to :action =>'index'
end
step:4
in media.rb file
has_attachment :content_type => :image,
:storage => :file_system,
:min_size => 0,
:max_size => 1.megabytes,
:resize_to => '34x38',
:thumbnails => { :thumb => '15x15>' },
:processor => 'MiniMagick',
:path_prefix => "public/medias/"
validates_as_attachment
step:5
In views
views/medias/index.rhtml
<% for media in @medias %>
<%= link_to image_tag(media.public_filename(:thumb)),media.public_filename %>
<%= link_to 'Remove',{:action=> 'delete',:id => media.id},:confirm=> "Are you sure?" , :method => :post %>
<% end %>
views/medias/new.rhtml
<% form_for(:media,:url => {:action => :create }, :html => {:multipart => true }) do |f| %>
Select a photo to upload
Photo: <%= f.file_field 'uploaded_data' %>
<%= submit_tag 'Upload Photo' %>
views/medias/show.rhtml
<%= image_tag(@media.public_filename) %>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment