I'm trying to add photo uploads to an Advert model using Strong Params with the Paperclip gem, where Photo is a separate model which "has_attached_file" :upload, and I am calling forms_for (actually semantic_forms_for as I'm using Formtastic) in the Ads#new view to upload the image to the Photo instance. Params looks ok but I think I've missed something in the controller. Haven't been able to get it working despite multiple iterations of the controller code. What would I need to do differently to get it working?
Would appreciate any tips or pointers! Thank you
--
class Ad < ActiveRecord::Base
belongs_to :user
##edited out irrelevant associations
has_many :photos, dependent: :destroy
accepts_nested_attributes_for :photos, :allow_destroy => true
default_scope { order("created_at DESC") }
#validates_presence_of :user_id, :make, :model, :km, :year, :location
end
--
class Photo < ActiveRecord::Base
belongs_to :ad
has_attached_file :upload, :styles => { :main => "600x500>",
:thumb => "60x45>" },
:default_url => "http://placehold.it/600x500&text=nice+wheels!"
end
class AdsController < ApplicationController
def create
@ad = Ad.new(ad_params)
@ad.user_id = current_user.id
@photo = @ad.photos.build
if @ad.save
redirect_to @ad
else
flash.alert="You were missing some super-important details. Try again"
redirect_to new_ad_path
end
end
private
def ad_params ##excluded all attributes except nested upload attrib. for gist
params.require(:ad).permit(photos_attributes: [upload: [:upload_file_name, :upload_content_type]])
end
end
<%= semantic_form_for @ad do |form| %>
<!-- left out all other form fields -->
<%= form.semantic_fields_for :photo do |photo| %>
<%= photo.file_field :upload %>
<% end %>
<% end %>
{"location":"Abha","make":"Bentley","model":"","gear":"","km":"50","price_bid":"500","price_ask":"500","ac":"0","cd":"0","radio":"0","powersteering":"0","abs":"0","satnav":"0","cruise":"0","fwd":"0","convertible":"0","problem_1":"","problem_2":"","problem_3":"","description":"","photo":{"upload":{"original_filename":"mustang-290x218.jpg","content_type":"image/jpeg","headers":"Content-Disposition: form-data; name=\"ad[photo][upload]\"; filename=\"mustang-290x218.jpg\"\r\nContent-Type: image/jpeg\r\n","tempfile":[]}}}
resources :ads, only: [:new, :create, :show, :index, :edit, :destroy] do
resources :comments, only: [:create, :destroy]
resources :photos, only: [:create, :destroy]
resources :favorite_ads, only: [:create, :destroy]
resource :emails, only: :create
end
The form needs to be specified as multi-part for file uploads to work:
<%= semantic_form_for @ad, :html => {:multipart => true} do |form| %>
In addition, you aren't permitting the tempfile to be passed:
params.require(:ad).
permit(photos_attributes:
[upload:
[:upload_file_name, :upload_content_type, :tempfile]
])
params.require(:ad).permit(photos_attributes: :upload)
this should be enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With