2015-11-16 2 views
0

Я пытаюсь получить имя файла загруженного файла через форму. 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 

ответ

0

код вы вывесили не ссылается params в любом месте, и нет способа узнать, какой объект techsupporters есть. Поэтому я не могу точно сказать, как изменить код.

Но из параметров, которые вы отправили, я считаю, что вы хотите params['test_scripts_attributes']['0']['script_name'].original_filename.

В конце концов, метод, который вы ищете, - ActionDispatch::Http::UploadedFile#original_filename.

С учетом этого параметры могут содержать что угодно: от нуля до нескольких файлов. Поэтому обязательно учтите это.

Смежные вопросы