4

これが回答されていて、見つけられなかった場合はお詫び申し上げます。どんな方向性でも大歓迎です。

Rails 4.1.4、Paperclip 4.2.0、Simple Form 3.0.2 を使用。

の後Submithas an extension that does not match its contentsフォームのエラー メッセージに出力が表示されます。

サーバーウィンドウで:

Started POST "/routes" for 127.0.0.1 at 2014-08-28 15:18:25 +0700
Processing by RoutesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"5BCHGBkwQH4mlnTVjy/PpD53mJKJpSmBXwXT/oul7yY=", "route"=>{"track_attributes"=>{"gpx"=>#<ActionDispatch::Http::UploadedFile:0x007fa89c9cd348 @tempfile=#<Tempfile:/var/folders/_g/6shs5yrj36n960wpt880ysl80000gn/T/RackMultipart20140828-42106-vi71nb>, @original_filename="Serge's tracks.gpx", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"route[track_attributes][gpx]\"; filename=\"Serge's tracks.gpx\"\r\nContent-Type: application/octet-stream\r\n">}, "title"=>"Serge track", "description"=>"loop of hang dong", "distance"=>"", "total_ascent"=>""}, "commit"=>"Create Route"}
Command :: file -b --mime '/var/folders/_g/6shs5yrj36n960wpt880ysl80000gn/T/f55fe48e09c9cc3ee6c6271fe94f407520140828-42106-1hgpby7.gpx'
[paperclip] Content Type Spoof: Filename Serge's_tracks.gpx ([]), content type discovered from file command: application/xml. See documentation to allow this combination.
(0.3ms)  BEGIN
Command :: file -b --mime '/var/folders/_g/6shs5yrj36n960wpt880ysl80000gn/T/f55fe48e09c9cc3ee6c6271fe94f407520140828-42106-62bkvh.gpx'
[paperclip] Content Type Spoof: Filename Serge's_tracks.gpx ([]), content type discovered from file command: application/xml. See documentation to allow this combination.
(0.8ms)  ROLLBACK

Paperclip docs で上記のドキュメントを見つけることができませんでした。ランニングfile Serge\'s\ tracks.gpx --mime-type -bプロデュースapplication/xml

私のMVCは次のようになります。

class Track < ActiveRecord::Base
  belongs_to :route
  has_attached_file :gpx
  validates_attachment_content_type :gpx, :content_type => /application\/xml/
end

class Route < ActiveRecord::Base
  has_one :track, dependent: :destroy
  accepts_nested_attributes_for :track
  validates :title, presence: true
end

中身RoutesController

def new
  @route       = Route.new
  @route.track = Track.new
end

def create
  @route = Route.new(route_params)
end

def route_params
  params.require(:route).permit(:title, :description, :distance, :total_ascent, track_attributes: [:gpx])
end

simple_form:

= simple_form_for @route do |r|
  = r.simple_fields_for :track do |t|
    = t.input :gpx
  = r.input :title
  = r.input :description
  = r.input :distance
  = r.input :total_ascent
  = r.button :submit
4

1 に答える 1

8

この投稿で述べたように: Paperclip gem spoofing error? この記事http://robots.thoughtbot.com/prevent-spoofing-with-paperclipfile -b --mime-typeでは、 Paperclip によって呼び出されるコマンドを明らかにバイパスすることで問題が解決されました。

これを行うために、 でpaperclip.rbファイルを作成しましたconfig/initializers

Paperclip.options[:content_type_mappings] = {
  :gpx => 'application/xml'
}

file問題は解決しましたが、コマンドが正しい結果を返したときになぜ問題が発生したのかについてまだ混乱して@content_type="application/octet-stream"おり、パラメーターのどこから来ているのかも知りたいです。

于 2014-08-29T09:52:24.160 に答える