简体   繁体   中英

How to add validation to refile within a rails app?

I have rails app that I created so I could use the API portion of things. I can successfully upload a file to the DB of the rails app using curl, but I can't figure out how I could limit the filetype / content type to just 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 #controller

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

I can't see anything wrong with your code, so this may be a bug in Refile. The only thing I can suggest is a work around using a custom validator.

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

You may want to use the file extension instead because the content_type is not available sometimes.

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

I wouldn't trust the client on this stuff, but even Refile depends on the client for content_type so it makes little different.

So there ended up being a couple of problems.

First, the strong parameters in the controller should look like the following,

def csv_params
    params.permit(:csv_file)
end

Secondly, I needed to add a column in a migration as such,

add_column :csv_files, :csv_file_id, :string

Finally, I was able to modify the csv_file.rb model file and add the following line.

attachment :csv_file, extension: "csv"

As it stands now, only files with an extension of .csv can be uploaded to the API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM