【问题标题】:How validate <%= file_field_tag "images[]", type: :file, multiple: true %>如何验证 <%= file_field_tag "images[]", type: :file, multiple: true %>
【发布时间】:2015-09-26 09:57:42
【问题描述】:

我有两个模型

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 pictures image blank?,我每次都会得到一个错误,图片是空白的,即使我选择了图片。

如何验证 has_many 与多个图像字段的关联?

参数

 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

【问题讨论】:

  • 你能展示你的画廊控制器吗?
  • 你能检查服务器日志是否有未经允许的参数警告吗?

标签: ruby-on-rails ruby-on-rails-3 validation ruby-on-rails-4


【解决方案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 模型将images 传递给Picture 模型:

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

这应该适合你。上传的多重方面,我不确定。

【讨论】:

  • 我开始得到Unknown key: allow_blank
  • 对不起,我不应该在那里
  • 我删除了它,但得到了undefined method field_for' #<:helpers>
  • 它应该是fields_for 在你的new.html.erb 中替换为这个 在
  • 抱歉,又是一个错字
猜你喜欢
  • 1970-01-01
  • 2011-11-12
  • 2016-08-12
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
相关资源
最近更新 更多