在 irb 中模拟为 Active Storage 选择文件
Simulating selecting a file for Active Storage in irb
正在为 Active Storage 创建一个新项目。
<!-- app/views/docs/_form.html.erb -->
<%= f.label :image, "Select document or image that support this information." %>
<%= f.file_field :image %>
我在单击 Create
时遇到 Please review the problems below
错误,我想看看 irb 中发生了什么错误。但是如何模拟上面的步骤呢
型号:
# models/doc.rb
class Doc < ApplicationRecord
has_one_attached :image # Active Storage
belongs_to :source
belongs_to :docable, polymorphic: true
# models/source.rb
class Source < ApplicationRecord
has_many :docs
# models/year.rb
class Year < ApplicationRecord
belongs_to :location
belongs_to :person
has_many :docs, as: :docable
# models/person.rb
class Person < ApplicationRecord
has_many :years, dependent: :destroy
has_many :locations, through: :years
has_many :docs, as: :docable
# models/location.rb
class Location < ApplicationRecord
has_many :years
has_many :people, through: :years
has_many :docs, as: :docable
一个人在某个日期生活或工作的地方以年为单位。年、人和地点使用 doc 来显示该信息的参考。
来源是一本旧书的书名,我正在对书中的各个页面进行成像。稍后我使用 docable 引用这些图像(这是计划)。
db/structure.sql: CREATE INDEX index_docs_on_docable_type_and_docable_id ON public.docs USING btree (docable_type, docable_id);
这是session:
irb(main):100:0> doc = Doc.new
=> #<Doc id: nil, source_id: nil, page_no: nil, original_url: nil, basename: nil, created_at: nil, updated_at: nil, notes: nil, docable_id: nil, docable_type: nil>
irb(main):101:0> doc.save
=> false
irb(main):102:0> doc.errors.messages
=> {:source=>["must exist"], :docable=>["must exist"]}
irb(main):104:0> doc.source_id = 4
=> 4
irb(main):105:0> doc.save
(42.8ms) BEGIN
Source Load (45.3ms) SELECT "sources".* FROM "sources" WHERE "sources"."id" = LIMIT [["id", 4], ["LIMIT", 1]]
(0.2ms) ROLLBACK
=> false
irb(main):106:0> doc.errors.messages
=> {:docable=>["must exist"]}
irb(main):107:0> doc.image =
我可能对多态关系有问题,所以我正在尝试解决这个问题。
https://edgeguides.rubyonrails.org/active_storage_overview.html#attaching-file-io-objects
doc.image.attach(io: File.open('/path/to/file'), filename: 'file.jpg')
正在为 Active Storage 创建一个新项目。
<!-- app/views/docs/_form.html.erb -->
<%= f.label :image, "Select document or image that support this information." %>
<%= f.file_field :image %>
我在单击 Create
时遇到 Please review the problems below
错误,我想看看 irb 中发生了什么错误。但是如何模拟上面的步骤呢
型号:
# models/doc.rb
class Doc < ApplicationRecord
has_one_attached :image # Active Storage
belongs_to :source
belongs_to :docable, polymorphic: true
# models/source.rb
class Source < ApplicationRecord
has_many :docs
# models/year.rb
class Year < ApplicationRecord
belongs_to :location
belongs_to :person
has_many :docs, as: :docable
# models/person.rb
class Person < ApplicationRecord
has_many :years, dependent: :destroy
has_many :locations, through: :years
has_many :docs, as: :docable
# models/location.rb
class Location < ApplicationRecord
has_many :years
has_many :people, through: :years
has_many :docs, as: :docable
一个人在某个日期生活或工作的地方以年为单位。年、人和地点使用 doc 来显示该信息的参考。 来源是一本旧书的书名,我正在对书中的各个页面进行成像。稍后我使用 docable 引用这些图像(这是计划)。
db/structure.sql: CREATE INDEX index_docs_on_docable_type_and_docable_id ON public.docs USING btree (docable_type, docable_id);
这是session:
irb(main):100:0> doc = Doc.new
=> #<Doc id: nil, source_id: nil, page_no: nil, original_url: nil, basename: nil, created_at: nil, updated_at: nil, notes: nil, docable_id: nil, docable_type: nil>
irb(main):101:0> doc.save
=> false
irb(main):102:0> doc.errors.messages
=> {:source=>["must exist"], :docable=>["must exist"]}
irb(main):104:0> doc.source_id = 4
=> 4
irb(main):105:0> doc.save
(42.8ms) BEGIN
Source Load (45.3ms) SELECT "sources".* FROM "sources" WHERE "sources"."id" = LIMIT [["id", 4], ["LIMIT", 1]]
(0.2ms) ROLLBACK
=> false
irb(main):106:0> doc.errors.messages
=> {:docable=>["must exist"]}
irb(main):107:0> doc.image =
我可能对多态关系有问题,所以我正在尝试解决这个问题。
https://edgeguides.rubyonrails.org/active_storage_overview.html#attaching-file-io-objects
doc.image.attach(io: File.open('/path/to/file'), filename: 'file.jpg')