【问题标题】:accepts_nested_attributes_for not saving paperclip imageAccepts_nested_attributes_for 不保存回形针图像
【发布时间】:2011-07-23 15:33:20
【问题描述】:

我正在尝试通过嵌套模型保存图像

**型号:

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

这里我只提到了视图中的一个字段,但是我使用了很多字段,除了数据(图像)字段之外,所有字段都保存在数据库中。

如果我验证照片模型中是否存在数据,我会收到“照片不应为空”消息,尽管我上传了一张图片。

【问题讨论】:

    标签: ruby-on-rails paperclip nested-forms


    【解决方案1】:

    除非您拥有多部分表单,否则您将无法上传图片 将 :html => { :multipart => true } 声明添加到 form_for 声明中,这样你就会得到类似这样的东西

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

    【讨论】:

    • 在这个问题上浪费了半天时间……即使一年前我还在为同样的问题苦苦挣扎。愚蠢的我......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 2016-03-31
    • 2020-10-14
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多