【问题标题】:Rails + Paperclip: Nested attributes file_field form helper not working?Rails + Paperclip:嵌套属性file_field表单助手不起作用?
【发布时间】:2013-10-17 22:07:57
【问题描述】:

我有一个表格:

    <%= 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 gem 尝试它时,它似乎不起作用:

    >> 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

【问题讨论】:

  • 您使用的是哪个版本的 Rails?

标签: ruby-on-rails paperclip nested-attributes


【解决方案1】:

-> 您的表单中需要 f.fields_for :)

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

-> 你需要允许传递嵌套的属性参数

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

-> 构建对象

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

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

【讨论】:

  • 但是当我执行f.fields_for 时,该字段不再出现
  • 那是因为你还没有构建对象。让我编辑我的答案!
  • 太棒了!!!谢谢。但是,您编写的 Category 模型中的第二部分 (category_image_attributes: [:attachment]) 不起作用。它仍然引发了质量分配错误,我必须将其更改为 :category_image_attributes
  • 我的错 - 我习惯了 Rails 4,我的语法本来是有效的 :) 为了后代,我会改变我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多