如何在我的Rails项目中添加Refile Gem

时间:2019-02-15 04:48:37

标签: ruby-on-rails ruby amazon-s3 refile

我正在使用AWS S3进行图像存储,并且Rails应用程序具有确认视图。但是在我的上传图片中(创建操作之前)未显示在我的确认视图中。然后,我尝试将Refile gem添加到我的应用程序中以使用S3的缓存,但是效果不佳。那么如何使Refile适应我的应用程序。

我正在使用Rails 5.1.6.1。在适应Refile之前,我使用的是Carrierwave,我认为它们具有相同的功能,所以我不知道如何使用Carrierwave适应Refile。在阅读正式文件Refile时,我发现image列的名称已更改为image_id,因此我制作了将文件更改为image_id的迁移文件,但它根本不起作用。

宝石文件

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.1'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
  gem 'dotenv-rails'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'pry-rails'
gem 'better_errors'
gem 'binding_of_caller'
gem 'rails_12factor', group: :production
gem 'bootstrap-sass', '3.3.6'
gem 'jquery-rails'
gem 'bcrypt', '3.1.11'
gem 'faker'
gem 'carrierwave'
gem 'mini_magick'
gem "font-awesome-rails"
gem 'fog',                     '1.40.0'

gem 'refile', github: 'refile/refile', require: 'refile/rails'
gem 'refile-mini_magick', github: 'refile/refile-mini_magick'
gem 'sinatra', github: 'sinatra/sinatra', branch: 'master'
gem "refile-s3"

group :development do
  gem 'letter_opener_web'
end

post.rb

class Post < ApplicationRecord
  belongs_to :user
  # imageカラムに、ImageUploaderを紐付けます。
  validates :content, presence: true, length: { maximum: 200 }
  validates :user_id, presence: true
  mount_uploader :image, ImageUploader
  has_many :favorites, dependent: :destroy
  # dependent: :destroy とすることで、ブログが削除されると同時に、お気に入り情報も削除されるようになります。
  has_many :favorite_users, through: :favorites, source: :user
#   has_many :favorite_users, through: :favorites, source: :userと定義することで、favorite_users というメソッドにより、 blogのidと一致するblog_idを持つfavoritesテーブルのレコードを全て取り出します。
# その上で、そのfavoritesテーブルのレコードがもっているuser_idの数字と同じidを持つuserを全件取得できるようになります。
# ここで言うuserはmodelクラスの名前?

  validates :content, presence: true, length: { in: 1..200 }
  # validates :image, presenve: true

  # validate  :image_size
  # 追加した独自バリデーション
  attachment :image


  private

  def image_size
      if image_id.size > 5.megabytes
        errors.add(:image, "should be less than 5MB")
      end
  end

end

我希望删除carrierwave.rb并将refile.rb制作到/ config / initializer。但是我不确定如何编写连接S3存储桶的方法。

这是我通过stackoverflow提出的第一个问题,如果您对此一无所知,很抱歉。

0 个答案:

没有答案