回形针未在我继承的模型上存储文件
(我是回形针新手)
我有这个模型层次结构:
基本模型:
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
新视图(使用 formattastic):
<%= semantic_form_for @imgD do |form| %>
<%= form.input :img%>
<%= form.actions %>
<%end%>
显示视图:
<%= image_tag @imgD.img.url %>
当我使用新视图(并选择要上传的文件)时,POST 正在工作,但没有保存/附加文件,“img_file_size”、“img_file_name”、 “img_content_type”设置为零。
如果我尝试显示它,结果是“缺失”字段。
编辑:
如果我尝试从控制台创建 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
=> #
(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
=> #
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要定义您的帖子,其中包含一些带有多部分 HTML 标记的文件
You need define your post contain some file inside with multipart HTML tag
非常愚蠢的错误:
没有任何意义......正确的路线是
really stupid mistake:
means nothing... right line is