Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Capistrano和ActiveStorage部署后不显示图像_Ruby On Rails_Capistrano_Symlink_Spree_Rails Activestorage - Fatal编程技术网

Ruby on rails 使用Capistrano和ActiveStorage部署后不显示图像

Ruby on rails 使用Capistrano和ActiveStorage部署后不显示图像,ruby-on-rails,capistrano,symlink,spree,rails-activestorage,Ruby On Rails,Capistrano,Symlink,Spree,Rails Activestorage,我已经在Digital Ocean的服务器上运行了Spree,但每次部署新版本时,都找不到用户在以前版本上上传的图像 根据我发现的相关帖子,我设置符号链接目录的方式似乎有问题 我尝试将storagedir添加到我的deploy.rb中的linked_dir中,并发布了一个运行cap production deploy的新版本,但仍然收到相同的错误 set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle .bun

我已经在Digital Ocean的服务器上运行了Spree,但每次部署新版本时,都找不到用户在以前版本上上传的图像

根据我发现的相关帖子,我设置符号链接目录的方式似乎有问题

我尝试将
storage
dir添加到我的
deploy.rb
中的linked_dir中,并发布了一个运行
cap production deploy
的新版本,但仍然收到相同的错误

set :linked_dirs,  %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle .bundle public/system public/uploads storage}
我真的不知道从当前版本开始这是否起作用,但问题是以前的图像仍然丢失,我需要在部署新版本后保留它们

这是我的
Capfile

# Load DSL and Setup Up Stages
require 'capistrano/setup'
require 'capistrano/deploy'

require 'sshkit/sudo'

require 'capistrano/rails/migrations'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
require 'capistrano/nginx'

install_plugin Capistrano::Puma

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
这是我的
config/deploy.rb

# config valid for current version and patch releases of Capistrano
lock "~> 3.14.0"

# Change these
server 'XXX.XX.XX.XXX', port: 22, roles: [:web, :app, :db], primary: true

set :repo_url,        'git@gitlab.com:my-user/my-app-name.git'
set :application,     'my-app-name'
set :user,            'deploy'
set :puma_threads,    [4, 16]
set :puma_workers,    0

# Don't change these unless you know what you're doing
set :pty,             true
set :use_sudo,        false
set :deploy_via,      :remote_cache
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :ssh_options,     { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true  # Change to false when not using ActiveRecord


## Linked Files & Directories (Default None):
set :linked_files, %w{config/secrets.yml config/database.yml}
set :linked_dirs,  %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle .bundle public/system public/uploads storage}

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
      end
    end
  end

  desc 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

  # Reload Nginx after deploy
  after 'deploy:published', 'nginx:reload'

  before :starting,     :check_revision
  # after  :finishing,    :precompile_assets
  after  :finishing,    :cleanup
  # after  :finishing,    :restart
end
Rails.application.configure do
  config.cache_classes = true
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable serving static files from the `/public` folder by default since
  # Apache or NGINX already handles this.
  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = true

  # Store uploaded files on the local file system (see config/storage.yml for options).
  config.active_storage.service = :local

  # Use the lowest log level to ensure availability of diagnostic information
  # when problems arise.
  config.log_level = :debug

  # Prepend all log lines with the following tags.
  config.log_tags = [ :request_id ]

  config.action_mailer.perform_caching = false

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation cannot be found).
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners.
  config.active_support.deprecation = :notify

  # Use default logging formatter so that PID and timestamp are not suppressed.
  config.log_formatter = ::Logger::Formatter.new

  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false

  Rails.application.routes.default_url_options[:protocol] = 'https'
end
这是我的
config/environments/production.rb

# config valid for current version and patch releases of Capistrano
lock "~> 3.14.0"

# Change these
server 'XXX.XX.XX.XXX', port: 22, roles: [:web, :app, :db], primary: true

set :repo_url,        'git@gitlab.com:my-user/my-app-name.git'
set :application,     'my-app-name'
set :user,            'deploy'
set :puma_threads,    [4, 16]
set :puma_workers,    0

# Don't change these unless you know what you're doing
set :pty,             true
set :use_sudo,        false
set :deploy_via,      :remote_cache
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :ssh_options,     { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true  # Change to false when not using ActiveRecord


## Linked Files & Directories (Default None):
set :linked_files, %w{config/secrets.yml config/database.yml}
set :linked_dirs,  %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle .bundle public/system public/uploads storage}

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
      end
    end
  end

  desc 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

  # Reload Nginx after deploy
  after 'deploy:published', 'nginx:reload'

  before :starting,     :check_revision
  # after  :finishing,    :precompile_assets
  after  :finishing,    :cleanup
  # after  :finishing,    :restart
end
Rails.application.configure do
  config.cache_classes = true
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable serving static files from the `/public` folder by default since
  # Apache or NGINX already handles this.
  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = true

  # Store uploaded files on the local file system (see config/storage.yml for options).
  config.active_storage.service = :local

  # Use the lowest log level to ensure availability of diagnostic information
  # when problems arise.
  config.log_level = :debug

  # Prepend all log lines with the following tags.
  config.log_tags = [ :request_id ]

  config.action_mailer.perform_caching = false

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation cannot be found).
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners.
  config.active_support.deprecation = :notify

  # Use default logging formatter so that PID and timestamp are not suppressed.
  config.log_formatter = ::Logger::Formatter.new

  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false

  Rails.application.routes.default_url_options[:protocol] = 'https'
end
这是我的
config/storage.yml

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>
测试:
服务:磁盘
根目录:
本地:
服务:磁盘
根目录:
相关的:


我有这个设置。在配置/存储中

  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

staging:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

production:
  service: Disk
  root: <%= Rails.root.join("../shared/storage") %>
服务:磁盘
根目录:
登台:
服务:磁盘
根目录:
本地:
服务:磁盘
根目录:
制作:
服务:磁盘
根目录:
以及在环境/生产中
config.active\u storage.service=:生产

以及在环境/发展方面
config.active\u storage.service=:开发


请注意,产品有../shared/storage

我在Ubuntu 20.04上也有同样的错误。问题不在于symlinked dir,而在于ImageMagick。 我在按照以下说明重新安装ImageMagick和所有附加组件后解决了此问题:

#These are the steps required in order to Install ImageMagick with JPG, PNG and TIFF delegates.

sudo apt-get update 

#Install Build-Essential in order to configure and make the final Install

sudo apt-get install build-essential 

#libjpg62-dev required in order to work with basic JPG files

sudo apt-get install -y libjpeg62-dev 

#libtiff-dev is required in order to work with TIFF file format

sudo apt-get install -y libtiff-dev 

#Download ImageMagick

wget https://www.imagemagick.org/download/ImageMagick.tar.gz 

#Untar Imagemagick

tar xvzf ImageMagick.tar.gz 

#Access the working directory

cd ImageMagick/[version_number] 

#Configure and make sure to disable the "shared" option

./configure --disable-shared

#Make

sudo make

#Install

sudo make install

#Final Check

sudo make check

好的,我终于做到了。问题是图像保存在服务器上的每个发行版文件夹中,因此每次我部署新版本(发行版上的新文件夹)时,图像都保留在上一个发行版的文件夹中。解决方案是在我的
config/storage.yml
中将图像存储到所有发行版之间共享的文件夹中,在本例中,我使用了此路径
root:
在此之后,我使用
mv./storage/*./../shared/storage/*.
将服务器上的所有图像移动到此新文件夹,现在每个图像都上载到此新路径,部署新版本时没有任何问题:)