You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 6 API部署Railway时遇assets:precompile任务不存在错误

在Railway部署Rails 6 API应用时阻止assets:precompile任务执行的方法

我尝试在Railway平台部署Rails 6 API应用,却持续遭遇以下错误:

#18 [stage-0 14/14] RUN  bundle exec rake assets:precompile
#18 sha256:20d51ba84b3edc1879a0fe36e13a5cfe583beb59329a77a09bdbd9289c2403d7
 
#18 4.000 rake aborted!
#18 4.000 Don't know how to build task 'assets:precompile' (See the list of available tasks with `rake --tasks`)
#18 4.000 /usr/local/rvm/gems/ruby-2.7.2/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
#18 4.000 /usr/local/rvm/gems/ruby-2.7.2/bin/ruby_executable_hooks:22:in `eval'
#18 4.000 /usr/local/rvm/gems/ruby-2.7.2/bin/ruby_executable_hooks:22:in `<main>'
#18 4.000 (See full trace by running task with --trace)
 
#18 ERROR: executor failed running [/bin/bash -ol pipefail -c bundle exec rake assets:precompile]: exit code: 1
-----
> [stage-0 14/14] RUN  bundle exec rake assets:precompile:
-----
executor failed running [/bin/bash -ol pipefail -c bundle exec rake assets:precompile]: exit code: 1
 
Error: Docker build failed

我的application.rb文件内容如下:

require_relative "boot"

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
# require "sprockets/railtie"
require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module AnimeAxisApi
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.1

    # Configuration for the application, engines, and railties goes here.
    #
    # These settings can be overridden in specific environments using the files
    # in config/environments, which are processed later.
    #
    # config.time_zone = "Central Time (US & Canada)"
    # config.eager_load_paths << Rails.root.join("extras")

    # Only loads a smaller set of middleware suitable for API only apps.
    # Middleware like session, flash, cookies can be added back manually.
    # Skip views, helpers and assets when generating a new resource.
    config.api_only = true

    config.middleware.use ActionDispatch::Cookies
    config.middleware.use ActionDispatch::Session::CookieStore
    config.action_dispatch.cookies_same_site_protection = :none 

    # goes after cookie store
    # , key: '_cookie_name'
  end
end

我已在lib/tasks中添加以下任务,但仍无法阻止assets:precompile任务执行:

Rake::Task["assets:precompile"].clear
   namespace :assets do
     task 'precompile' do
     puts "Not pre-compiling assets..."
   end

我知道这是因为API-only应用无需编译资产,此前在Heroku部署时未出现该问题。现询问如何阻止该任务执行?


解决方法

1. 修改Railway的构建命令

Railway默认会为Rails应用自动添加assets:precompile到构建流程中,直接修改构建命令即可跳过该步骤:

  • 进入项目的Settings -> Build
  • 将默认构建命令替换为只包含必要步骤,例如:
    bundle install --without development test && rails db:migrate
    

2. 完善Rake任务并禁用资产功能

  • 补全lib/tasks/assets.rake中的任务(添加缺失的end),确保正确覆盖原有任务:
    # lib/tasks/assets.rake
    if Rake::Task.task_defined?("assets:precompile")
      Rake::Task["assets:precompile"].clear
    end
    
    namespace :assets do
      task :precompile do
        puts "Skipping assets precompilation for API-only application"
      end
    end
    
  • 在application.rb中添加配置,彻底禁用Rails的资产功能:
    # config/application.rb
    config.assets.enabled = false
    

3. 检查Gemfile依赖

确保Gemfile中没有引入sprockets、rails_assets等会触发资产编译的gem,避免Rails自动加载相关任务。


内容的提问来源于stack exchange,提问作者Zio-4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 17:55:20