所以我一直在尝试测试我的模型,看看我是否可以使用Gallery.new(:picture => File.new('E:\Owner (Backup)\Pictures\Skateboard 1.jpg', 'r'))
通过rails控制台添加图片,但我得到以下错误。我正在这样做,因为我无法在我的rails应用程序中保存图片,我只是重新呈现new
模板和新视图中列出的表单部分。
我想知道如何解决这些错误,因为我可以看到我的模型很好。我的迁移似乎也很好。我认为accept_nested_attributes_for
允许Picture
模型属性通过Gallery
模型传递。
控制台中的错误是
irb(main):002:0> Gallery.new(:picture => File.new('E:\Owner (Backup)\Pictures\Skateboard 1.jpg', 'r'))
ActiveModel::UnknownAttributeError: unknown attribute 'picture' for Gallery.
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activemodel-5.0.1/lib/active_model/attribute_assignment.rb:48:in `_assign_attribute'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activemodel-5.0.1/lib/active_model/attribute_assignment.rb:40:in `block in _assign_attributes'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activemodel-5.0.1/lib/active_model/attribute_assignment.rb:39:in `each'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activemodel-5.0.1/lib/active_model/attribute_assignment.rb:39:in `_assign_attributes'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/attribute_assignment.rb:26:in `_assign_attributes'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activemodel-5.0.1/lib/active_model/attribute_assignment.rb:33:in `assign_attributes'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/core.rb:319:in `initialize'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/inheritance.rb:65:in `new'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/inheritance.rb:65:in `new'
from (irb):2
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in `start'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'irb(main):003:0> ^C
irb(main):003:0>
我的模特是画廊
class Gallery < ApplicationRecord
has_many :pictures
validates :name, presence: true
validates :cover, presence: true
accepts_nested_attributes_for :pictures, allow_destroy: true
end
照片
class Picture < ApplicationRecord
belongs_to :gallery
validates :gallery_id, presence: true
validates :picture, presence: true
mount_uploader :picture, PictureUploader
serialize :picture, JSON
end
迁移,画廊
class CreateGalleries < ActiveRecord::Migration[5.0]
def change
create_table :galleries do |t|
t.string :name
t.integer :cover
t.timestamps
end
end
end
图片
class CreatePictures < ActiveRecord::Migration[5.0]
def change
create_table :pictures do |t|
t.integer :gallery_id
t.string :picture
t.timestamps
end
end
end
答案 0 :(得分:2)
ActiveModel :: UnknownAttributeError:未知属性&#39;图片&#39;对于 库。
你做错了。您应 关联 图片 与 图库实例 而是将其作为 属性传递给图库模型 。
g = Gallery.new(name: 'Some Name', cover: 'Some Cover')
g.save!
p = g.pictures.new(picture: File.new('E:\Owner (Backup)\Pictures\Skateboard 1.jpg', 'r'))
p.save!