1

我有一個兩個模型如何驗證<%= file_field_tag 「圖像[]」,類型:文件,多個:真%>

Gallery 

class Gallery < ActiveRecord::Base 
    has_many :pictures, :dependent => :destroy 

    accepts_nested_attributes_for :pictures, :allow_destroy => true 

    attr_accessible :name, :description, :pictures_attributes 

    validates_presence_of :pictures 
end 

Picture 

    class Picture < ActiveRecord::Base 
     belongs_to :gallery 

     has_attached_file :image, 
     :path => ":rails_root/public/images/:id/:filename", 
     :url => "/images/:id/:filename" 
    end 

並形成

<%= form_for @gallery, :html => { :class => 'form-horizontal', multipart: true } do |f| %> 
    ......... 
    <div class="controls"> 
      <%= file_field_tag "images[]", type: :file, multiple: true %> 
     </div> 
    ......... 
    <% end %> 

控制器

def create 
     @gallery = Gallery.new(gallery_params) 

     respond_to do |format| 
      if @gallery.save 

      if params[:images]   
       params[:images].each { |image| 
       @gallery.pictures.create(image: image) 
       } 
      end 

      format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' } 
      format.json { render json: @gallery, status: :created, location: @gallery } 
      else 
      format.html { render action: "new" } 
      format.json { render json: @gallery.errors, status: :unprocessable_entity } 
      end 
     end 
     end 
private 

    def gallery_params 
    params.require(:gallery).permit(:description, 
            :name, 
            :pictures 
            ) 
    end 

如果我想驗證has_many圖片圖像是否空白,我每次都得到一個圖片爲空的錯誤,即使我選擇了圖片。

我如何驗證has_many與多個圖像字段的關聯?

PARAMS

Parameters: {"utf8"=>"✓", "authenticity_token"=>"MCkZFvsYvRndlfaqwXiw5MfhSHGoesawEAPFWzn0Ulw=", "gallery"=>{"name"=>"", "description"=>""}, "images"=>[#<ActionDispatch::Http::UploadedFile:0x007fb7ad9cb7d0 @tempfile=#<Tempfile:/var/folders/r4/jqx7vz8d48z2ky3ndpyzv9vc0000gn/T/RackMultipart20150926-38180-1xrchqg>, @original_filename="yamaha-blaster-BOARD-HEROa.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"images[]\"; filename=\"yamaha-blaster-BOARD-HEROa.jpg\"\r\nContent-Type: image/jpeg\r\n">], "commit"=>"Create Gallery"} 
    (0.1ms) begin transaction 
    (0.0ms) rollback transaction 
+0

您可以顯示您的圖庫控制器嗎? – IngoAlbers

+0

您可以檢查服務器日誌是否允許未經許可的參數警告? – IngoAlbers

回答

1

你的問題就出在你使用的accepts_nested_attributes_for - 你的參考模型是pictures,而你在你的代碼參照images

這裏是你應該怎麼做:

#app/models/picture.rb 
class Picture < ActiveRecord::Base 
    belongs_to :gallery 
    validates :image, presence: true 

    has_attached_file :image, 
     :path => ":rails_root/public/images/:id/:filename", 
     :url => "/images/:id/:filename" 
end 

#app/models/gallery.rb 
class Gallery < ActiveRecord::Base 
    has_many :pictures 
    accepts_nested_attributes_for :pictures, allow_destroy: true 
end 

你越來越感到困惑的image對象和Picture模型。 IE瀏覽器,你需要通過你的Gallery模型傳遞imagesPicture型號:

#app/controllers/galleries_controller.rb 
class GalleriesController < ApplicationController 
    def new 
     @gallery = Gallery.new 
     @gallery.pictures.build 
    end 

    def create 
     @gallery = Gallery.new gallery_params 
     @gallery.save 
    end 

    private 

    def gallery_params 
     params.require(:gallery).permit(:description, :name, pictures_attributes: [:images]) 
    end 
end 

這意味着你將不得不使用f.fields_for,這可能會非常棘手。

這裏是如何做到這一點:

#app/views/galleries/new.html.erb 
<%= form_for @gallery do |f| %> 
    <%= f.text_field :name %> 
    <%= f.fields_for :pictures do |picture| %> 
     <%= picture.file_field type: :file, multiple: true %> 
    <% end %> 
    <%= f.submit %> 
<% end %> 

這應該爲你工作。 多個方面的上傳,我不太確定。

+0

我開始得到'未知鍵:allow_blank' – Erihon1890

+0

對不起是我不好,不應該一直在那裏 –

+0

我刪除它,但得到'未定義的方法'field_for」爲#<::的ActionView Helpers' – Erihon1890

相關問題