Upload an image


#1

can anyone give me the code for upload an image,i try
in various ways such as acts_as_attatchment plugin but
it wasn’t work…now im in a big trouble,plz give me another
solution to this


#2

the view:

<% form_for(@user, :url => (@edition ? user_path() :
user_path), :method => :put, :html => {:multipart => true}) do |f| %>
<%= f.file_field(:user_image_file) %>

<%= submit_tag(‘Save’) %>
<% end %>

in the controller:

if params[:user][:user_image_file] then
  @user.upload_image
end

the model:

def upload_image
if user_image_file && user_image_file != “” then
file = user_image_file
write_attribute(:image_mime_type,
MIME::Types.type_for(file.original_filename).to_s.gsub(‘image/’, ‘’))
File.open(File.join(IMAGE_FILE_LOCATION,
image_filename(:original)), ‘wb’) { |f| f.write(file.read) }

  save
end

end