나는 형태가 있습니다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
어떤 레일 버전을 사용하고 계십니까? 귀하의 개체가 단 하나이기 때문에 "build_"도우미 대신 "build_"도우미를 사용해야한다고 생각합니다. –
레일을 사용하고 있습니다. 3 – Edmund