【问题标题】:Trying to add the Paperclip gem to rails project尝试将 Paperclip gem 添加到 rails 项目
【发布时间】:2015-04-05 05:27:02
【问题描述】:

我正在关注video tutorial,以便在我的 rails 项目中设置 Paperclip gem。

所以到目前为止我采取的步骤..

  • 在终端上安装 ImageMagick - brew install imagemagick
  • 安装了 Paperclip gem - gem "paperclip", "~> 4.2"
  • 在我的 house.rb 模型文件中添加了以下内容:

    has_attached_file:图像,样式:{ 大:“600x600”,中:“300x300”,拇指:“150x150#”} validates_attachment_content_type :image, content_type: /\Aimage/.*\Z/

所以我的 house.rb 模型文件现在看起来像这样:

class House < ActiveRecord::Base
validates :title, presence: true
validates :price, presence: true
validates :description, presence: true
validates :image, presence: true

has_attached_file :image, styles: { large: "600x600", medium: "300x300", thumb: "150x150#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

此时我需要使用迁移生成器添加迁移,所以我输入 rails g migration house image 并且它没有在文件夹中创建带时间戳的数据库迁移文件并且终端正在返回:

Usage:
rails new APP_PATH [options]

还有一大堆选项。在本教程中,它应该返回如下内容:

create db/migrate/20150205123408_add_attachment_image_to_posts.rb

我不确定我在哪里出错了,而且我是 ruby​​-on-rails 的新手,所以非常感谢任何帮助!

【问题讨论】:

  • 当你运行生成器时,你之前运行过bundle install 并且在你的rails 项目目录中? Usage: rails new APP_PATH 帮助仅在不在 Rails 根目录中时出现。
  • Rookie 错误,我已经完成了捆绑安装,是的,但我不在正确的目录中。我道歉。感谢您的帮助!

标签: ruby-on-rails gem paperclip


【解决方案1】:

如果问题仍然存在,请手动创建迁移。

 create_table "media", force: :cascade do |t|
    t.string   "image_video_file_name"
    t.string   "image_video_content_type"
    t.integer  "image_video_file_size"
    t.datetime "image_video_updated_at"
    t.string   "text", default: ""
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id", null: false
  end

  add_index "media", ["user_id"], name: "index_media_on_user_id", using: :btree

【讨论】:

  • 问题是目录错误,新手错误。不过,双重注意手动迁移。谢谢!
猜你喜欢
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多