简体   繁体   English

accepts_nested_attributes_for不保存回形针图像

[英]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. 如果我在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