Muhammed Bhikha
Muhammed Bhikha

Reputation: 4999

Rails: Image upload (WIthout plugin's)

I have a table which stores information about sauces. Each sauce has an image inside the images assets folder, inside a folder called sauces. All sauces files are named the same; eg assets/images/sauces/sauces_piri.png

All I want to do is basically upload a .png file in the form where the creation takes place, and inside the field of pic_url the name of the image is stored along with the sauces/ so it is directed correctly when I want to display the image.

Currently the administrator has to physically upload the image in the correct position using the domain file management, and also input the "sauces/sauces_name.png" when creating the new sauce.

The form for adding a new sauce :

<%= error_messages_for(@sauce) %>
   <table summary="Sauces Form Fields">
    <tr>
     <th><%= f.label(:name,"Sauce Name") %></th>
     <td><%= f.text_field(:name) %></td>
    </tr>
    <tr>
     <th><%= f.label(:description, "Description") %></th>
     <td><%= f.text_area(:description, :size => '40x5') %></td>
    </tr>
    <tr>
     <th><%= f.label(:heat_level, "Heat Level") %></th>
     <td><%= f.select(:heat_level,{ 1 => "1", 2 => "2", 3 => "3", 4 => "4", 5 => "5"}) %></td>
   </tr>
   <tr>
    <th><%= f.label(:pic_url, "Picture URL") %></th>
    <td><%= f.text_field(:pic_url) %></td>
   </tr>
   <tr>
    <th><%= f.label(:title_colour, "Title Colour") %></th>
    <td><%= f.text_field(:title_colour) %></td>
   </tr>
   <tr>
    <th><%= f.label(:description_colour, "Desc Colour") %></th>
    <td><%= f.text_field(:description_colour) %></td>
   </tr>
  </table>

So without using plug-ins such as paperclip how do I enable an image upload which then the file is stored in the correct place, and also in the table field pic_url the foldername/filename.png is stored?

Upvotes: 3

Views: 627

Answers (1)

Sully
Sully

Reputation: 14943

It is not clear to me what you are having issues with. So, I will post a sample form on uploading files.

<%= form_for(:uploaded_data_file, :url => upload_files_path(:params => params) ,  :remote => true, :html => { :multipart => true } ) do |f| %>
  <%= f.label "Upload" %><br />
  <%= f.file_field :location %>
<% end %>

You will have to define the path for the function that will store the image in this example it is called upload_files_path and we are passing to it all the params. Then restart the webapp to get the new routes.

In the controller, you can save the file and its details

To get the file name

params[:uploaded_data_file][:location].original_filename

To get the file itself and save it

File.open("where/to/save", "wb") { |f| f.write(params[:uploaded_data_file][:location].read) }

To make sure its a .png, you can do some regex checks

if(name =~ /.png$/i) # for more than one type do (name =~ /.jpeg$|.png$/i)

To do something else look in your params and make the desired changes.

For the route to work you can look at http://edgeguides.rubyonrails.org/routing.html#adding-more-restful-actions

resources :posts do
  collection do
    get :upload_files # will create upload_files_posts_path
  end
end

Or

match '/upload_files', :to => 'controller_name#method_name' # 'posts#upload_files'

Or

<% form_tag({:action => 'upload_file'}  #will use the correct controller based on the form

Upvotes: 2

Related Questions