为活动存储创建一个新项目。
<!-- app/views/docs/_form.html.erb -->
<%= f.label :image, "Select document or image that support this information." %>
<%= f.file_field :image %>
我得到了一个 Please review the problems below
当我点击 Create
并想看看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引用这些图像(这是计划)。
dbstructure.sql。CREATE INDEX index_docs_on_docable_type_and_docable_id ON public.docs USING btree (docable_type, docable_id);
这里是会议。
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" = $1 LIMIT $2 [["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.orgactive_storage_overview.html#attaching-file-io-objects。
doc.image.attach(io: File.open('/path/to/file'), filename: 'file.jpg')