回形针未在我继承的模型上存储文件

发布于 2025-01-05 21:47:42 字数 2187 浏览 1 评论 0原文

(我是回形针新手)

我有这个模型层次结构:

基本模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2025-01-12 21:47:42

您需要定义您的帖子,其中包含一些带有多部分 HTML 标记的文件

<%= semantic_form_for @imgD, :html => { :multipart => true } do |form| %>
  <%= form.input :img%>
  <%= form.actions %>
<%end%>

You need define your post contain some file inside with multipart HTML tag

<%= semantic_form_for @imgD, :html => { :multipart => true } do |form| %>
  <%= form.input :img%>
  <%= form.actions %>
<%end%>
窝囊感情。 2025-01-12 21:47:42

非常愚蠢的错误:

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

没有任何意义......正确的路线是

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

really stupid mistake:

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

means nothing... right line is

@imgD=ImageDescription.new(params[:image_description])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文