首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Carrierwave文件上传在ActiveRecord Create上验证失败

Carrierwave文件上传在ActiveRecord Create上验证失败
EN

Stack Overflow用户
提问于 2012-12-10 21:59:54
回答 4查看 3.4K关注 0票数 4

所以我有一个非常简单的Carrierwave图片上传实现。我已经将上传程序挂载到了ActiveRecord模型上:

代码语言:javascript
复制
class MenuItem < ActiveRecord::Base
    attr_accessible :description, :menu_id, :name, :active, :photo

    mount_uploader :photo, PhotoUploader

    belongs_to :menu

    before_save :default_values

    validates :name, presence: true
    validates :menu_id, presence: true
    validates :description, length: { maximum: 250 }

    private
       def default_values
         if (self.active.nil?)
           self.active = true
         end
       end  
end

我使用的表单将编辑/创建上面的MenuItem与附加文件结合在一起:

代码语言:javascript
复制
    <%= form_for(@menuitem) do |f| %>
        <%= render 'shared/error_messages', object: @menuitem %>

        <%= f.file_field :photo, :id => "photo" %>

        <%= f.label :name %>
        <%= f.text_field :name %>

        <%= f.label :description %>
        <%= f.text_area :description %>

        <%= f.label :active %>
        <%= f.check_box :active, :value => @menuitem.active %>

        <%= f.submit "Save Menu Item", class: "btn btn-large btn-primary" %>
        <%= f.submit "Cancel", class: "btn btn-large btn-secondary" %>
    <% end %>

一切工作都可以在编辑/更新的情况下找到。

然而,在New / Create上,我在:photo属性上得到了一个验证错误,它只是简单地说"Photo是无效的“。Uploader的定义很简单:

代码语言:javascript
复制
class PhotoUploader < CarrierWave::Uploader::Base
  include CarrierWaveDirect::Uploader

  after :store, :make_thumbnail

  storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def make_thumbnail(file)
    a = filename.split('/')
    key = store_dir + "/" + a[0] + "/" + model.id.to_s + "_thumb.jpg"

    job = Blitline::Job.new(url)
    job.application_id = ENV['BLITLINE_APPLICATION_ID']

    sf = job.add_function("resize_to_fill", {:height => 100, :width => 100})
    sf.add_save("thumbnail", key, ENV['S3_BUCKET'])

    bs = Blitline.new
    bs.jobs << job   
    res = bs.post_jobs
  end
end

而且Controller方法也很简单

代码语言:javascript
复制
class MenuItemsController < ApplicationController
  def new
    @menuitem = MenuItem.new
    @menuitem.menu_id = params[:menuid]
    @menuitem.active = true
  end

  def create
    @menuitem = MenuItem.new(params[:menu_item])

    if @menuitem.save
        flash[:success] = "Menu Item created!"
        redirect_to menu_path(@menuitem.menu)
    else
        render 'new'
    end    
end

我认为这个问题可能与上传器的store_dir方法中对model.id的引用有关,并且ID不可用,因为记录还没有创建-但删除它对问题没有帮助。

同样,使用相同表单并仅调用menuitem.update_attributes(params[:menu_item])的编辑/更新路径也可以正常工作。

我相信我肯定漏掉了什么。如有任何建议,我们将不胜感激!

EN

回答 4

Stack Overflow用户

发布于 2014-04-06 20:36:36

您可以通过将carrierwave.rb初始化器文件中的- ur_net_format验证设置为false来修复此错误。

像这样的

代码语言:javascript
复制
ruby
    config.validate_unique_filename = true
    config.validate_filename_format = true
    config.validate_remote_net_url_format = true

希望这对任何人都有帮助。

票数 1
EN

Stack Overflow用户

发布于 2012-12-10 23:36:15

我有个建议:

白名单格式(在您的上传器中):

代码语言:javascript
复制
  def extension_white_list
   %w(jpg jpeg gif png)
  end
票数 0
EN

Stack Overflow用户

发布于 2015-02-22 21:43:11

您需要在Carrierwave.rb文件中设置以下内容

代码语言:javascript
复制
  config.validate_unique_filename = false
  config.validate_filename_format = false
  config.validate_remote_net_url_format = false # this is important

这对我很有效。就我个人而言,我现在使用carrierwave-aws,因为它比Fog更精简。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13802430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档