Syk
Syk

Reputation: 393

Image Upload into Database

I try to upload two images for each object of a certain type following those instructions: http://archive.railsforum.com/viewtopic.php?id=4642

Thats what my models look like:

Models:

class Mepager < ActiveRecord::Base
    has_many :images
end

class Image < ActiveRecord::Base
    belongs_to :mepager

     def image_file=(input_data)
         self.filename = input_data.original_filename
         self.content_type = input_data.content_type.chomp
         self.binary_data = input_data.read
     end
end

Schema:

create_table "images", force: true do |t|
    t.string   "content_type"
    t.string   "filename"
    t.binary   "binary_data"
    t.string   "connector"
    t.integer  "mepager_id"
    t.datetime "created_at"
    t.datetime "updated_at"
end

Controller

def new
@mepager = @pimp.build_mepager
@pre_image = @mepager.images.build(:connector => "pre")
@post_image = @mepager.images.build(:connector => "post")
end

def mepager_params
  params.require(:mepager).permit(:id,
    :images_attributes => [:id, :content_type, :filename, :binary_data, :mepager_id, :connector])
end

Form:

<%= simple_form_for :mepager, url: mepager_path, html: {multipart: true} do |f| %> 


<%= f.simple_fields_for :images, @pre_image do |image| %>
    <b>Pre-Image</b>
        <%= image.input :image_file, :as => :file, label: false, input_html:{:size => 15} %>
        <%= image.input :connector %>               
<% end %>

<%= f.simple_fields_for :images, @post_image do |image| %>
    <b>Post-Image</b>
        <%= image.input :image_file, :as => :file, label: false, input_html:{:size => 15} %>
        <%= image.input :connector %>               
<% end %>
<% end %>

But if I choose a file in that form and submit it, its not getting saved!

Anyone?

Best regards!

Upvotes: 0

Views: 38

Answers (1)

Beckhar
Beckhar

Reputation: 124

Why don't you instead just save the image link to the database and then upload the image to a given directory on your server, its much better and secure. And for that to work you have to use paperclip look at this example Start by adding paperclip gem in the gem file like this

gem "paperclip", "~> 2.3"

After run bundle install in your project directory

Then generate a migration to add the field to your table like this

rails g migration add_attach_paperclip

Then add these columns to your table

class AddattachPaperclip < ActiveRecord::Migration
def self.up
add_column :images, :attach_file_name, :string
add_column :images, :attach_content_type, :string
add_column :images, :attach_file_size, :integer
add_column :images, :attach_updated_at, :datetime
end

def self.down
remove_column :images, :attach_file_name
remove_column :images, :attach_content_type
remove_column :images, :attach_file_size
remove_column :images, :attach_updated_at
end
end

The you go and rake you migration

After you change your model for images by adding this

has_attached_file :pic

After change the name of your file field to pic That is all you need when you want to display your image you just use

<%= @image.pic.url %>

Upvotes: 1

Related Questions