Rails Active Storage не загружает файлы - PullRequest
0 голосов
/ 31 мая 2019

Я совершенно новичок в Ruby On Rails.Я пытался сделать приложение, где я могу загружать файлы в локальное хранилище.Я использую ActiveStorage.После выполнения всех шагов я не могу загрузить файл.Я делюсь кодами, которые могут понадобиться, чтобы выручить меня.

  1. new.html.ebr
<h1>Add New Post</h1>
<%= form_for @test do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %><br>
  </p>
  <p>
    <%= f.label :file %><br>
    <%= f.file_field :file %><br>
  </p>
  <p>
    <%= f.submit "Add Post" %><br>
  </p>
<% end %>
test_controller.rb
class TestsController < ApplicationController
  def index
  end

  def new
    @test = Test.new
  end

  def create
    @test = Test.new(test_params)
    if @test.save
      redirect_to tests_path
    else
      redirect_to "new"
    end
  end

  def test_params
    params.require(:test).permit(:title, :file)
  end
end
test.rb
class Test < ApplicationRecord
    has_one_attached :file
end
модель-код миграции (на всякий случай)
class CreateTests < ActiveRecord::Migration[5.1]
  def change
    create_table :tests do |t|
      t.string :title
      t.string :file

      t.timestamps
    end
  end
end

После отправки формы обновляется только таблица tests.Значение в столбце file равно #<ActionDispatch::Http::UploadedFile:0x000000000d5bec50>, а не пути к файлу.И таблицы active_storage_attachments and active_storage_blobs, необходимые для обновления, не обновляются.

  • Я использую PostgreSQL V10.x в качестве базы данных.
  • Версия Ruby - ruby ​​2.5.3p105
  • Версия Rails - Rails 5.1.7

Редактировать: Iдобавляю журнал для дополнительной информации

Запущен POST "/ tests" для 127.0.0.1 в 2019-05-31 12:25:25 + 0530
Обработка в TestsController # создать как HTML
Параметры: {"utf8" => "✓", "authenticity_token" => "W9EE8ot + SzsJQMzgQ6gVWqER2TIXN2mUW7PBgTt0DV8mskeR2oGO / iHTx + quBT0cXTLobU60SoS" = "+"> ">"> ">"> ">"> ">"> ">"> ">"> ">" ""file "=> # '<' ActionDispatch :: Http:: UploadedFile: 0x000000000d0a25c8> @ tempfile = #, @ original_filename =" contacts.vcf ", @ content_type =" text / x-vcard ", @head ers =" Content-Расположение: form-data; name = \ "test [file] \"; filename = \ "contacts.vcf \" \ r \ nContent-Type: text / x-vcard \ r \ n ">}," commit "=> «Добавить сообщение»}
(0,1 мс) НАЧАТЬ
SQL (0,8 мс) ВСТАВИТЬ В «тесты» («заголовок», «файл», «create_at», «updated_at») ЗНАЧЕНИЯ ($ 1, $ 2,$ 3, $ 4) ВОЗВРАЩЕНИЕ "id" [["title", "sss"], ["file", "#"], ["creation_at", "2019-05-31 06: 55: 25.837658"], ["updated_at", "2019-05-31 06: 55: 25.837658"]]
(26,6 мс) COMMIT
Перенаправлено на http://localhost:3000/tests
Завершено 302 Найдено за 34 мс (ActiveRecord: 27,5 мс)

** Кто-нибудь может сказать, связана ли эта проблема с версией рельсов?

Заранее спасибо.

...