3
我試圖通過嵌套模式將圖像保存accepts_nested_attributes_for不節能回形針圖像
**型號:
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#"}
列表控制器:
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
觀點:
<%= form_for [@listing] do |f| %>
<%= f.fields_for :photos do |ph| %>
<%= ph.file_field :data %>
<% end%>
<%end%>
這裏我只提到了視圖中的一個字段,但是我使用了很多字段,並且除數據(圖像)字段外,所有字段都保存在數據庫中。
如果我在照片模型中驗證了數據的存在,但我上傳了一張圖片後,我得到了「照片不應該是空的」消息。
哦謝謝!現在它工作的很好... – arivarasan
很高興你得到它的工作 – jamesc
這一天失去了半天...即使當我在一年前同樣的問題掙扎。愚蠢的我... –