【问题标题】:Validate picture upload before create object创建对象前验证图片上传
【发布时间】:2016-08-01 12:29:35
【问题描述】:

所以我有一个有很多照片的汽车课。 并且在创建新车时,我需要确保用户确实先选择了一张图片。 这是实现。

class Car < ActiveRecord::Base
 has_many :photos
end

和班级照片:

class Photo < ActiveRecord::Base
belongs_to :car

has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

这是汽车表格的一部分:

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

            <div class="col-xs-6">
                <div class="form-group">
                    <label>Modele</label>
                    <%= f.text_field :modele, placeholder: "Modele", class: "form-control" %>
                </div>
            </div>
                <div class="row">
                <div class="col-md-4">
                    <div class="form-group">                            
                        <span class="btn btn-default btn-file">
                            <i class="fa fa-cloud-upload fa-lg"></i> Upload Photos 
                            <%= file_field_tag "images[]", type: :file, multiple: true %>
                        </span>
                    </div>      
                </div>
            </div>
   <% end %>

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-4 rubygems multipartform-data


【解决方案1】:

看看https://github.com/carrierwaveuploader/carrierwave/wiki/how-to:-validate-attachment-file-size

require 'file_size_validator' 
class Photo < ActiveRecord::Base
  belongs_to :car

  validates :image, presence: true
end

这应该可以解决问题

快乐的黑客

【讨论】:

  • 你知道你收到了什么信息吗?
  • 实际上没有消息,只创建了房间,没有限制:/
猜你喜欢
  • 2017-05-28
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 2016-05-26
  • 1970-01-01
  • 2023-03-05
  • 2015-03-13
相关资源
最近更新 更多