【问题标题】:Seeding a model with a paperclip image in ruby on rails在 ruby​​ on rails 中播种带有回形针图像的模型
【发布时间】:2015-04-08 18:29:40
【问题描述】:

我正在使用 Ruby on rails 和旁边的回形针来存储和链接图像。效果很好。

我现在想使用 rake db:seed 工具并使用“事件”对象填充我的种子.rb 文件。

我正在使用种子文件使用如下语法填充我的应用程序的其他区域:

categories = Category.create([{ name: 'General'}, {name: 'Clubs'}, {name: 'For Mum'}, {name: 'Services'}])

这是填充简单的类别模型,它只有 1 个字段 - 名称。如何为更复杂的模型创建种子,例如我的“事件”模型,它也需要图像作为其中的一部分?我可以以某种方式拥有一个种子图像目录并使用种子在本地加载文件吗?

您能否向我展示一个使用种子 rake 文件创建带有回形针图像字段的模型实例的解决方案示例。

谢谢!

【问题讨论】:

    标签: ruby-on-rails rake paperclip ruby-on-rails-plugins


    【解决方案1】:

    您可以像下面这样创建 ActiveRecord 模型。

    # create Event record with paperclip file
    Event.create(name: 'event1', photo: File.new("image/to/path.jpg"))
    

    这是事件模型:

    class Event < ActiveRecord::Base
      has_attached_file :photo
    end
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-12
      • 2013-04-15
      • 2017-01-14
      相关资源
      最近更新 更多