Search code examples
ruby-on-railspaperclipsingle-table-inheritancesti

Paperclip isn't storing file on my inherited model


(I am new to paperclip)

I have this model hierarchy:

base model:

class QuestDescription < ActiveRecord::Base
end

inherited model:

class ImageDescription < QuestDescription
  has_attached_file :img
end

and I'm using Single Table Inheritance from ActiveRecord

[part of] schema.rb:

create_table "quest_descriptions", :force => true do |t|
  t.string   "type"
  t.datetime "img_updated_at"
  t.integer  "img_file_size"
  t.string   "img_file_name"
  t.string   "img_content_type"
  t.datetime "created_at",         :null => false
  t.datetime "updated_at",         :null => false
end

ImageDescription controller:

class ImageDescriptionsController < ApplicationController

  def new
    @imgD = ImageDescription.new
  end

  def create
    @imgD=ImageDescription.new(params[:imgD])
    if @imgD.save
      redirect_to :back, :flash => {:notice => "saved"} 
    else
      redirect_to :back, :flash => {:error => "error"} 
    end
  end

  def show
    @imgD=ImageDescription.find(params[:id])
  end
end    

new view (using formtastic):

<%= semantic_form_for @imgD do |form| %>
  <%= form.input :img%>
  <%= form.actions %>
<%end%>

show view:

<%= image_tag @imgD.img.url %>

when i use the new view (and select the file to upload), the POST is working, but no file is being saved/attached, the "img_file_size", "img_file_name", "img_content_type" are set to nil.

If I try to show it, the result is a "missing" field.

EDIT:

if i try to create a ImageDscription from the console, it works:

ImageDescription.create(:img => File.new(Rails.root + "public/images/grid.png"))
    (0.1ms) begin transaction
    SQL (31.9ms) INSERT INTO "quest_descriptions" ("created_at", "img_content_type", "img_file_name", "img_file_size", "img_updated_at", "type", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) ["created_at", Wed, 15 Feb 2012 00:07:48 UTC +00:00], ["img_content_type", "image/png"], ["img_file_name", "grid.png"], ["img_file_size", 206], ["img_updated_at", Wed, 15 Feb 2012 00:07:48 UTC +00:00], ["type", "ImageDescription"], ["updated_at", Wed, 15 Feb 2012 00:07:48 UTC +00:00] commit transaction
    => #

Solution

  • really stupid mistake:

    @imgD=ImageDescription.new(params[:imgD])
    

    means nothing... right line is

    @imgD=ImageDescription.new(params[:image_description])