Forum: Ruby on Rails upload an image

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ishara G. (Guest)
on 2008-11-05 15:03
(Received via mailing list)
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
Thorsten M. (Guest)
on 2008-11-05 15:20
(Received via mailing list)
the view:

  <% form_for(@user, :url => (@edition ? user_path() :
user_path), :method => :put, :html => {:multipart => true}) do |f| %>
    <%= f.file_field(:user_image_file) %><br />
    <%= 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
This topic is locked and can not be replied to.