Ruby on rails 在irb中模拟为活动存储选择文件

Ruby on rails 在irb中模拟为活动存储选择文件,ruby-on-rails,debugging,polymorphism,rails-activestorage,irb,Ruby On Rails,Debugging,Polymorphism,Rails Activestorage,Irb,正在为活动存储创建新项 <!-- app/views/docs/_form.html.erb --> <%= f.label :image, "Select document or image that support this information." %> <%= f.file_field :image %> 一个人在某一日期居住或工作的地点以年为单位。年份、人员和地点使用文档显示该信息的参考。 来源是一本旧书的书名,我正在想象书中的各个页面

正在为活动存储创建新项

<!-- app/views/docs/_form.html.erb -->
  <%= f.label :image, "Select document or image that support this information." %>
  <%= f.file_field  :image %>
一个人在某一日期居住或工作的地点以年为单位。年份、人员和地点使用文档显示该信息的参考。 来源是一本旧书的书名,我正在想象书中的各个页面。稍后,我使用docable引用这些图像,这就是计划

db/structure.sql:使用btree docable\u type,docable\u id在\u docable\u type\u和public.docs上创建索引\u docs\u和\u docable\u 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 =
我可能在多态关系方面有问题,所以我正在努力解决这个问题


非常感谢。这证明我有很多东西要学,因为我读到了,错过了。不幸的是,当我使用没有错误的应用程序时,仍然会出现回滚错误。=>{:docable=>[必须存在]}。除非我弄明白了,否则这是另一个问题的主题。但它清楚地表明了我是如何建立多态性的。无论图像是否已设置,都应提示错误相同。默认情况下,需要“属于”关系。如果您不希望它是必需的,请添加optional:true。如果打算需要它,那么您需要将其设置在某个位置。到目前为止,您提供的代码没有设置为可在任何地方进行修改。谢谢。我想要必需的。但我设置多态关系的方式有点不对劲。我在OP中添加了其他关键模型。我可以在服务器日志或应用程序中显示错误消息而不是一般错误吗?现在您添加了模型,我可以更好地理解您试图实现的目标,但错误消息对我来说仍然很清楚。Docable必须存在且不存在,因为您在创建文档时没有在任何位置设置它。单据上需要可docable,当前没有docable单据不能存在。
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 =
doc.image.attach(io: File.open('/path/to/file'), filename: 'file.jpg')