accepts_nested_attributes_for not saving paperclip image
I am trying to save images through nested model
**model:
Listing
has_many:photos
accepts_nested_attributes_for :photos, :allow_destroy => true
Photo
belongs_to:listing
has_attached_file :data, :styles=>{:featured => "88x63#", :search_result => "122x91#"}
listings controller:
def new
@listing = Listing.new
@listing.photos.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @listing }
end
end
def create
开发者_如何学运维 @listing = Listing.new(params[:listing])
if @listing.save
redirect_to(:action=>'index')
else
render :action => "new"
end
end
view:
<%= form_for [@listing] do |f| %>
<%= f.fields_for :photos do |ph| %>
<%= ph.file_field :data %>
<% end%>
<%end%>
Here I mentioned only one field in the view, but I used lot of fields and all are saved in the database except the data (image) field.
If I validate the presence of data in the Photo model I got "photo should not be empty" message though I uploaded a image.
You won't be able to upload images unless you have a multi-part form Add :html => { :multipart => true } declaration to the form_for declaration so you get something like this
<%= form_for(@listing, :html => { :multipart => true }) do |f| %>
精彩评论