2013-10-17 55 views
0

我有一个表格:Rails + Paperclip:嵌套属性file_field表单助手不工作?

<%= form_for [:admin, @category], :html => { :multipart => true } do |f| %> 

    <%= f.collection_select(:parent_id, Category.all, :id, :title, include_blank: true) %><br /> 

    <%= f.label :title %><br /> 
    <%= f.text_field :title %><br /> 

    <%= f.label :description %><br /> 
    <%= f.text_area :description %><br /> 



    <%= fields_for :category_image do |category_image_fields| %> 
     <%= category_image_fields.file_field :attachment %><br /> 
    <% end %> 

    <%= f.submit %> 


    <% end %> 

但是当我冻结它,并尝试一下使用better_errors宝石似乎不工作:

>> params 
    => {"utf8"=>"✓", "authenticity_token"=>"JWT+P6OQHKQC+sx5ytNoyAaGEKwCHb15Mb0H7FSJlTM=", "category"=>{"parent_id"=>"3", "title"=>"ffff", "description"=>"ffff"}, "category_image"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x007fdd375e9590 @original_filename="bacon.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"category_image[attachment]\"; filename=\"bacon.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/nw/zzv_cpt94_18vhvmcwk26gh80000gr/T/RackMultipart20131017-37289-1k42gos>>}, "commit"=>"Create Category", "action"=>"create", "controller"=>"admin/categories"} 
    >> c=Category.new(params[:category]) 
    => #<Category id: nil, title: "ffff", description: "ffff", parent_id: 3, created_at: nil, updated_at: nil> 
    >> c.category_image 
    => nil 

这是我的模型使用回形针那代表形象:

class CategoryImage < Asset #<-- trying to reuse it for other models too 
    belongs_to :category 
end 



class Asset < ActiveRecord::Base 
    has_attached_file :attachment 
    validates_attachment_presence :attachment 
    validates_attachment_content_type :attachment, 
            :message => "Please upload correct format", 
            :content_type => %w(image/jpeg image/png image/pjpeg image/x-png image/gif) 

    has_attached_file :attachment, :styles => { :small => '100' } 

    attr_accessible :attachment 
end 

,这是我的分类模型应该has_one图像:

class Category < ActiveRecord::Base 
    attr_accessible :title, :description, :category_image, :parent_id 

    has_many :subcategories, class_name: 'Category', foreign_key: 'parent_id', dependent: :destroy 
    belongs_to :parent_category, class_name: 'Category', foreign_key: 'parent_id' 

    has_many :books 
    has_one :category_image 
    accepts_nested_attributes_for :category_image 

    validates_presence_of :title, :description 
end 
+0

其中的Rails的版本是您使用? –

+0

我正在使用rails 3 – Edmund

回答

3

- >您的表单:)

<%= f.fields_for :category_image do |category_image_fields| %> 
    <%= category_image_fields.file_field :attachment %><br /> 
<% end %> 

需要f.fields_for - >您需要允许嵌套属性PARAMS传递

class Category < ActiveRecord::Base 
    attr_accessible :title, :description, :category_image, :parent_id, :category_image_attributes 

- >构建对象

在您的控制器操作(通常是新的,但可能是一个nother),您需要构建f.fields_for将要定位的对象。我相信你应该使用“build_”帮手,而不是“.build”因为你的对象是单数:

def new 
    @category = Category.new 
    @category.build_category_image 
end 
+0

但是当我做'f.fields_for'字段不再出现 – Edmund

+0

那是因为你还没有建立对象。让我编辑我的答案! –

+0

太棒了!谢谢。但是,您编写的'Category'模型中的第二部分('category_image_attributes:[:attachment]')不起作用。它仍然抛出一个大规模的分配错误,我不得不改变它:'category_image_attributes'而不是 – Edmund