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.
您将无法上传图像,除非您有一个多部分表单添加:html => {:multipart => true}声明到form_for声明,所以你得到这样的东西
<%= form_for(@listing, :html => { :multipart => true }) do |f| %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.