Rails 4 - 使用carrierwave从硬盘驱动器播种图像

时间:2014-06-30 12:33:37

标签: ruby-on-rails carrierwave

我试图用carrierwave播种图片。在查看了一些问题,包括this之后,我尝试过:

c1 = Category.create(
  name: "About Us",
  page_title: "12 Years of Tradition",
  page_description: "long description here",
  page_content: "a lot of content here",
  link_to_subcategory: "Meet the Robinson's",
  banner_image: open("RF_web_about_us_1.jpg"),
  main_image: open("RF_web_about_us_2.jpg"),
  home_page_content: "content here"
  )

不幸的是,当我运行rake db:seed

时出现以下错误
rake aborted!
Errno::ENOENT: No such file or directory - RF_web_about_us_1.jpg

图像(RF_web_about_us_1.jpg)存储在app / assets / images中。对此有任何帮助将非常感激。

2 个答案:

答案 0 :(得分:4)

另一种方法:

Rails.root.join("app/assets/images/RF_web_about_us_1.jpg").open

所以你的create语句如下:

c1 = Category.create(
  name: "About Us",
  page_title: "12 Years of Tradition",
  page_description: "long description here",
  page_content: "a lot of content here",
  link_to_subcategory: "Meet the Robinson's",
  banner_image: Rails.root.join("app/assets/images/RF_web_about_us_1.jpg").open,
  main_image: Rails.root.join("app/assets/images/RF_web_about_us_2.jpg").open,
  home_page_content: "content here"
)

参考:https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-%22Upload%22-from-a-local-file

答案 1 :(得分:3)

您必须指定图像的完整路径:

File.open(Rails.root +" app / assets / images / RF_web_about_us_1.jpg")