7

Gần đây tôi đã cập nhật Rails của mình lên 3.1.Hình ảnh hiển thị dưới dạng trống trong Rails 3.1 về Sản xuất (Heroku)

Dưới đây là một phần mà tôi nói thêm:

<%= asset_path('logo_symbol.png') %> 

này làm cho /assets/logo_symbol.png mà làm việc hoàn toàn tốt đẹp trong môi trường phát triển. Tuy nhiên, khi tôi đẩy mã để sản xuất trên heroku, nó cho thấy một hình ảnh bị hỏng, với url: assets/logo_symbol-135ddc8db2c9b59f032bed7db520137a.png. Tôi đoán tên mới là vì lý do tối ưu hóa. Tuy nhiên, điều thú vị cần lưu ý là khi tôi đi đến địa chỉ assets/logo_symbol-135ddc8db2c9b59f032bed7db520137a.png trên sản xuất, tôi thấy một trang trống, nhưng khi tôi thay đổi url đó thành bất kỳ điều gì ngẫu nhiên, như thêm số vào nó, nó hiển thị một trang không được tìm thấy. Vì vậy, rõ ràng nó là tìm kiếm một cái gì đó trên url đó. Nó cũng hiển thị một trang trống khi tôi đi đến /assets/logo_symbol.png trực tiếp trên sản xuất/heroku.

Nếu đây là bất kỳ trợ giúp nào, heroku không biên dịch trước thành công khi tôi đẩy mã và tài liệu của heroku nói rằng hiện tại không có xung đột cho vấn đề đó.

Bất kỳ trợ giúp nào trong điều này sẽ được đánh giá cao.

Tôi đoán là nó có liên quan đến một số cấu hình liên quan đến môi trường. Tôi gắn nội dung của application.rb tôi, development.rb và các tập tin production.rb

đây là những nội dung của file production.rb tôi

# Settings specified here will take precedence over those in config/application.rb 

    # In the development environment your application's code is reloaded on 
    # every request. This slows down response time but is perfect for development 
    # since you don't have to restart the webserver when you make code changes. 
    config.cache_classes = false 

    # Log error messages when you accidentally call methods on nil. 
    config.whiny_nils = true 

    # Show full error reports and disable caching 
    config.consider_all_requests_local  = true 
    config.action_controller.perform_caching = false 

    # Don't care if the mailer can't send 

    #config.action_mailer.default_url_options = { :host => 'localhost:3000' } 
    #config.action_mailer.default_url_options = { :host => 'localhost:3000' } 
    config.action_mailer.delivery_method = :smtp 

    # Print deprecation notices to the Rails logger 
    config.active_support.deprecation = :log 

    # Only use best-standards-support built into browsers 
    config.action_dispatch.best_standards_support = :builtin 

    # Do not compress assets 
    config.assets.compress = false 

    # Expands the lines which load the assets 
    config.assets.debug = true 
end 

module ActiveAdmin 
    class Reloader 
    def attach! 
    end 
    end 
end 

và đây là nội dung của tập tin development.rb của tôi

# Settings specified here will take precedence over those in config/application.rb 

    # In the development environment your application's code is reloaded on 
    # every request. This slows down response time but is perfect for development 
    # since you don't have to restart the webserver when you make code changes. 
    config.cache_classes = false 

    # Log error messages when you accidentally call methods on nil. 
    config.whiny_nils = true 

    # Show full error reports and disable caching 
    config.consider_all_requests_local  = true 
    config.action_controller.perform_caching = false 

    # Don't care if the mailer can't send 

    #config.action_mailer.default_url_options = { :host => 'localhost:3000' } 
    #config.action_mailer.default_url_options = { :host => 'localhost:3000' } 
    config.action_mailer.delivery_method = :smtp 

    # Print deprecation notices to the Rails logger 
    config.active_support.deprecation = :log 

    # Only use best-standards-support built into browsers 
    config.action_dispatch.best_standards_support = :builtin 

    # Do not compress assets 
    config.assets.compress = false 

    # Expands the lines which load the assets 
    config.assets.debug = true 
end 

module ActiveAdmin 
    class Reloader 
    def attach! 
    end 
    end 
end 

Dưới đây là nội dung của tập tin production.rb tôi

# Settings specified here will take precedence over those in config/application.rb 

    # The production environment is meant for finished, "live" apps. 
    # Code is not reloaded between requests 
    config.cache_classes = true 

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

    # Specifies the header that your server uses for sending files 
    config.action_dispatch.x_sendfile_header = "X-Sendfile" 

    # For nginx: 
    # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' 

    # If you have no front-end server that supports something like X-Sendfile, 
    # just comment this out and Rails will serve the files 

    # See everything in the log (default is :info) 
    # config.log_level = :debug 

    # Use a different logger for distributed setups 
    # config.logger = SyslogLogger.new 

    # Use a different cache store in production 
    # config.cache_store = :mem_cache_store 

    # Disable Rails's static asset server 
    # In production, Apache or nginx will already do this 
    config.serve_static_assets = false 

    # Enable serving of images, stylesheets, and javascripts from an asset server 
    # config.action_controller.asset_host = "http://assets.example.com" 

    # Disable delivery errors, bad email addresses will be ignored 
    # config.action_mailer.raise_delivery_errors = false 

    #config.action_mailer.default_url_options = { :host => 'ha1.heroku.com' } 
    config.action_mailer.delivery_method = :smtp 

    # Enable threaded mode 
    # config.threadsafe! 

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

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

    # Compress JavaScripts and CSS 
    #config.assets.compress = true 

    # Don't fallback to assets pipeline if a precompiled asset is missed 
    config.assets.compile = false 

    # Generate digests for assets URLs 
    config.assets.digest = true 

    # Defaults to Rails.root.join("public/assets") 
    # config.assets.manifest = YOUR_PATH 

    config.assets.js_compressor = :uglifier 
    config.assets.css_compressor = :scss 

Tôi đã so sánh các tập tin cấu hình của tôi với các tài liệu đường ray cho 3.1 và có vẻ như tôi có tất cả các giá trị mặc định cần thiết. Tuy nhiên tôi vẫn không thấy hình ảnh. Bất kỳ trợ giúp sẽ được nhiều đánh giá cao

Trả lời

13

Hủy bỏ dòng này từ production.rb:

 
config.action_dispatch.x_sendfile_header = "X-Sendfile" 

Bạn cũng nên sắp xếp các thiết lập trong các tập tin cấu hình của bạn với những người trong section 9 of the pipeline guides.

Tiêu đề gửi có chứa thông tin cho máy chủ web ngược dòng nơi tìm tệp (trên hệ thống tệp) để phân phát. Điều này loại bỏ tải từ backend (Rails/Sprockets). Khi sendfile là trên các phản ứng HTTP không chứa cơ thể (nó là số không chiều dài) đó là lý do tại sao bạn thấy không có gì.

Trên heroku, máy chủ nginx không có quyền truy cập vào hệ thống tệp ứng dụng, vì vậy thao tác này sẽ không hoạt động.

Xem this note trên trang gửi lại của trang web dev của Heroku.

Nếu bạn đang sử dụng heroku, this document nêu ra các tùy chọn tốt nhất để sử dụng đường ống một cách hiệu quả.

+0

Tôi đã xóa dòng đó và cũng đảm bảo rằng các dòng được đề cập trên trang được liên kết có trong tệp của tôi. Tôi vẫn có cùng một vấn đề trên trang web live heroku, nơi nó không hiển thị hình ảnh. Bất kỳ ý tưởng tại sao? – alik

+1

hmm thực sự, làm xước nó, nó chỉ hoạt động. Tôi nghĩ rằng nó đã được cacheing trước đó – alik

+0

Bạn nói đúng, vấn đề của tôi là bộ nhớ đệm. Sau khi xóa bộ nhớ cache của trình duyệt, nó hoạt động tốt. – Ryan

0

Bạn cần làm hai việc để giải quyết. Đầu tiên, thay đổi hai dòng này thành false thành true trong tệp production.rb.

 config.assets.compile = true 
     config.assets.digest = true 

Thứ hai, nếu bạn đã cú pháp như thế này cho hình ảnh của bạn

background: url("imgo.jpg") 

Thay đổi nó để

 background: image-url("image.jpg") 

Tôi hy vọng nó làm công việc của bạn.

Các vấn đề liên quan