1

(我是回形针新手)

我有这个模型层次结构:

基本型号:

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

图像描述控制器:

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 正在工作,但没有保存/附加文件,“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
    => #
4

2 回答 2

0

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

<%= semantic_form_for @imgD, :html => { :multipart => true } do |form| %>
  <%= form.input :img%>
  <%= form.actions %>
<%end%>
于 2012-02-15T14:50:40.010 回答
0

非常愚蠢的错误:

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

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

@imgD=ImageDescription.new(params[:image_description])
于 2012-02-15T17:28:52.443 回答