通过表单访问文件名

时间:2015-11-16 01:10:08

标签: ruby-on-rails ruby

我正在尝试通过表单获取上传文件的文件名。 params返回如下数据:

(byebug) pp params
{"enable_test"=>"1",
 "tokens_per_day"=>"0",
 "unlimited_tokens"=>"0",
 "test_scripts_attributes"=>
  {"0"=>
    {"seq_num"=>"1.0",
     "script_name"=>
      #<ActionDispatch::Http::UploadedFile:0x00000007b370c0
       @content_type="application/pdf",
       @headers=
        "Content-Disposition: form-data; name=\"assignment[test_scripts_attributes][0][script_name]\"; filename=\"ass5import.pdf\"\r\nContent-Type: application/pdf\r\n",
       @original_filename="ass5import.pdf",
       @tempfile=#<File:/tmp/RackMultipart20151115-5586-patihn.pdf>>,
     "description"=>"",
     "max_marks"=>"2",
     "run_on_submission"=>"0",
     "run_on_request"=>"0",
     "halts_testing"=>"0",
     "display_description"=>"display_after_submission",
     "display_run_status"=>"display_after_submission",
     "display_marks_earned"=>"display_after_submission",
     "display_input"=>"display_after_submission",
     "display_expected_output"=>"display_after_submission",
     "display_actual_output"=>"display_after_submission"}}}
{"enable_test"=>"1", "tokens_per_day"=>"0", "unlimited_tokens"=>"0", "test_scripts_attributes"=>{"0"=>{"seq_num"=>"1.0", "script_name"=>#<ActionDispatch::Http::UploadedFile:0x00000007b370c0 @tempfile=#<Tempfile:/tmp/RackMultipart20151115-5586-patihn.pdf>, @original_filename="ass5import.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"assignment[test_scripts_attributes][0][script_name]\"; filename=\"ass5import.pdf\"\r\nContent-Type: application/pdf\r\n">, "description"=>"", "max_marks"=>"2", "run_on_submission"=>"0", "run_on_request"=>"0", "halts_testing"=>"0", "display_description"=>"display_after_submission", "display_run_status"=>"display_after_submission", "display_marks_earned"=>"display_after_submission", "display_input"=>"display_after_submission", "display_expected_output"=>"display_after_submission", "display_actual_output"=>"display_after_submission"}}}

现在,我试图修复的代码是存储script_name作为文件名,这是错误的/我想存储文件名。如何访问@headers文件名?

代码我试图修改以前访问过它,但似乎这已经过时且不再有用(代码来自RoR 3.0)

# Filter out test support files that need to be created and updated
    unless testsupporters.nil?
      testsupporters.each_key do |key|
        tfile = testsupporters[key]

        # Check to see if this is an update or a new file:
        # - If 'id' exists, this is an update
        # - If 'id' does not exist, this is a new test file
        tf_id = tfile['id']

        # If only the 'id' exists in the hash, other attributes were not updated
        # so we skip this entry. Otherwise, this test file possibly requires an
        # update
        if !tf_id.nil? && tfile.size > 1

          # Find existing test file to update
          @existing_testsupport = TestSupportFile.find_by_id(tf_id)
          if @existing_testsupport
            # Store test file for any possible updating
            updated_support_files[key] = tfile
          end
        end

        # Test file needs to be created since record doesn't exist yet
        if tf_id.nil? && tfile['file_name']
          updated_support_files[key] = tfile
        end
      end
    end

1 个答案:

答案 0 :(得分:0)

您发布的代码未在任何地方引用params,也无法知道对象techsupporters是什么类型。所以我无法确切地告诉你如何修改你的代码。

但是从你发布的params中,我相信你想要的是params['test_scripts_attributes']['0']['script_name'].original_filename

最后,您要查找的方法是ActionDispatch::Http::UploadedFile#original_filename

从它的外观来看,参数可以包含从零到多个文件的任何内容。所以一定要考虑到这一点。