bigpotato
bigpotato

Reputation: 27497

Rails + Paperclip: Nested attributes file_field form helper not working?

I have a form:

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

but when I freeze it and try it out using the better_errors gem it doesn't seem to work:

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

This is my model that uses paperclip that stands for the image:

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

and this is my Category model that should has_one image:

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

Upvotes: 0

Views: 2531

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

-> You need f.fields_for in your form :)

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

-> You need to allow the nested attribute params to be passed

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

-> Build the object

In your controller action (typically new, but could be another), you need to build the object the f.fields_for will target. I believe you should use the "build_" helper, rather than ".build" because your object is singular:

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

Upvotes: 3

Related Questions