(我是纸夹新手)
我有这个模型层次结构:
基础模型:
class QuestDescription < ActiveRecord::Base
end
继承模型:
class ImageDescription < QuestDescription
has_attached_file :img
end
我正在使用ActiveRecord的单表继承
[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控制器:
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
新观点(使用formtastic):
<%= semantic_form_for @imgD do |form| %>
<%= form.input :img%>
<%= form.actions %>
<%end%>
显示视图:
<%= image_tag @imgD.img.url %>
当我使用新视图(并选择要上传的文件)时,POST正在运行,但没有保存/附加文件,&#34; img_file_size&#34;,&#34; img_file_name&#34; ,&#34; img_content_type&#34;设为零。
如果我试图展示它,结果是&#34;缺失&#34;字段。
编辑:
如果我尝试从控制台创建ImageDscription,它可以工作:
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
=> #
答案 0 :(得分:0)
您需要使用多部分HTML标记定义您的帖子中包含一些文件
<%= semantic_form_for @imgD, :html => { :multipart => true } do |form| %>
<%= form.input :img%>
<%= form.actions %>
<%end%>
答案 1 :(得分:0)
真是个愚蠢的错误:
@imgD=ImageDescription.new(params[:imgD])
什么都不是......右边是
@imgD=ImageDescription.new(params[:image_description])