【问题标题】:How to add validation to refile within a rails app?如何在 Rails 应用程序中添加验证以重新归档?
【发布时间】:2016-01-16 09:26:42
【问题描述】:

我创建了 Rails 应用程序,因此我可以使用 API 部分。我可以使用 curl 成功地将文件上传到 rails 应用程序的数据库,但我不知道如何将文件类型/内容类型限制为 CSV。

csv_file.rb #model

class CsvFile < ActiveRecord::Base
    # attachment :content_type => "text/csv"
    # http://ryanbigg.com/2009/04/how-rails-works-2-mime-types-respond_to/
    attachment :csv, extension: "csv", content_type: "text/csv"
end

csv_files.rb #控制器

class API::V1::CsvFilesController < ApplicationController

  # see http://stackoverflow.com/questions/15040964/ for explanation
  skip_before_filter :verify_authenticity_token

  def index
    @csv_files = CsvFile.all
    if @csv_files
      render json: @csv_files,
        # each_serializer: PictureSerializer,
        root: "csv_files"
    else
      @error = Error.new(text: "404 Not found",
                          status: 404,
                          url: request.url,
                          method: request.method)
      render json: @error.serializer
    end 
  end

  def show
    if @csv_file
      render json: @csv_file,
              # serializer: PictureSerializer,
              root: "csv_file"
    else
      @error = Error.new(text: "404 Not found",
                          status: 404,
                          url: request.url,
                          method: request.method)
      render json: @error.serializer
    end
  end

  # POST /csv_files.json
  def create
    @csv_file = CsvFile.new(csv_params)

    if @csv_file.save
      render json: @csv_file,
        # serializer: PictureSerializer, 
        meta: { status: 201,
          message: "201 Created"},
          root: "csv_file"
    else
      @error = Error.new(text: "500 Server Error",
        status: 500,
        url: request.url,
        method: request.method)
      render :json => @error.serializer
    end
  end

  def update
  end

  def delete
  end

  private

  def csv_params

  end
end

【问题讨论】:

  • 你问这是否应该工作?或者你试过这个但没有用?
  • 当前代码已实现,但似乎无法正常工作,因为我可以上传非 CSV 文件。
  • 什么显示/告诉你它不起作用?
  • 我仍然可以上传非 CSV 文件。
  • 尝试将raise_errors: true 选项添加到attachment 定义中,看看会发生什么。

标签: ruby-on-rails ruby-on-rails-4 refile


【解决方案1】:

我看不出你的代码有什么问题,所以这可能是 Refile 中的一个错误。我唯一能建议的是使用自定义验证器。

validate :csv_extension

private

def csv_extension
  unless csv_content_type == "text/csv"
    errors.add :csv, "format must be csv" # might want to use i18n here.
  end
end

您可能希望使用文件扩展名,因为 content_type 有时不可用。

def csv_extension
  unless File.extname(csv_filename) == "csv"
    errors.add :csv, "format must be csv"
  end
end

我不会相信客户在这些东西上,但即使是 Refile 也依赖于content_type 的客户,所以它几乎没有什么不同。

【讨论】:

  • 我尝试将您的两个示例都放在我的 csv_file.rb 模型文件中,但 rails 仍然给我一个错误,NameError (undefined local variable or method csv_filename' for #): app/models/csv_file.rb:16:in csv_extension' app/controllers/api/v1/csv_files_controller.rb:41:in create'
  • 您的设置有问题。 Refile 将获取附件定义,在这种情况下为 csv,并将 _fulename 附加到它。 size 和 content_type 相同。所以我会仔细检查那里发生了什么。如果你上面的代码是正确的,你不应该从 Rails 得到这个错误。 ps:你是用master的gem吗?你用的是什么版本?
  • 在我的 Gemfile 用于 rails 应用程序中,我有以下几行,gem 'refile', require: 'refile/rails' gem 'refile-mini_magick' 您愿意在 chat.stackoverflow.com 上托管的 Ruby 聊天室中进一步讨论这个问题吗?
  • 你需要使用 Github 的 master。将第一行改为gem 'refile', github: 'refile/refile', require: 'refile/rails'
【解决方案2】:

所以最终出现了一些问题。

首先,控制器中的强参数应该如下所示,

def csv_params
    params.permit(:csv_file)
end

其次,我需要在迁移中添加一列,

add_column :csv_files, :csv_file_id, :string

最后,我能够修改csv_file.rb 模型文件并添加以下行。

attachment :csv_file, extension: "csv"

目前,只有扩展名为 .csv 的文件可以上传到 API。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多