簡體   English   中英

accepts_nested_attributes_for不保存回形針圖像

[英]accepts_nested_attributes_for not saving paperclip image

我試圖通過嵌套模型保存圖像

**模型:

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

這里我只提到了視圖中的一個字段,但是我使用了很多字段,除了數據(圖像)字段外,所有字段都保存在數據庫中。

如果我在Photo模型中驗證數據的存在,我得到了“照片不應該為空”的消息,盡管我上傳了一張圖片。

您將無法上傳圖像,除非您有一個多部分表單添加:html => {:multipart => true}聲明到form_for聲明,所以你得到這樣的東西

<%= form_for(@listing, :html => { :multipart => true }) do |f| %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM