10

Tôi đang cố gắng tạo tệp javascript tệp kê khai tùy chỉnh riêng biệt từ application.js. Tôi đã lấy mã từ application.js và dán nó vào một tệp mới mà tôi đã gọi là "other_manifest.js" và được đặt trong thư mục assets/javascrips. Đây là mã:Đường dẫn nội dung đường ray: cách tạo tệp kê khai tùy chỉnh

// This is a manifest file that'll be compiled into application.js, which will include all the files 
// listed below. 
// 
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. 
// 
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 
// compiled file. 
// 
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details 
// about supported directives. 
// 
//= require jquery 
//= require jquery_ujs 
//= require turbolinks 
//= require bootstrap 
//= require_tree . 

Trong file assets.rb của tôi, tôi đã bao gồm các dòng:

Rails.application.config.assets.precompile += %w(other_manifest.js) 

tôi precompile và làm sạch tài sản tại địa phương, và sau đó khi tôi chạy trang, tất cả tôi nhận được là văn bản chính xác từ tệp kê khai. Nó không được đưa vào bất kỳ tập tin nào. Làm cách nào để tạo tệp kê khai tùy chỉnh?

+0

bạn đã thay thế 'application.js' trong tệp' app/views/layouts/application.html' của bạn? – itsnikolay

+0

Tôi đặt vào một if/else tùy thuộc vào trang, application.js hiển thị hoặc other_manifest.js xuất hiện – Philip7899

Trả lời

2

Dễ dàng

Bạn có application.js. Tạo một tập tin thứ hai: other_manifest.js

Sau đó, trong cách bố trí của bạn layouts/application.html.erb (có thể là một cách bố trí khác nhau hoàn toàn):

<% if condition %> 
    <%= javascript_include_tag 'other_manifest' %> 
<% else %> 
    <%= javascript_include_tag 'application' %> 
<% end %> 

Và có, bạn cần phải thêm vào config/initializers/assets.rb của bạn (tiếp theo khởi động lại):

Rails.application.config.assets.precompile += %w(other_manifest.js) 

Ngoài ra, hãy đảm bảo xóa //= require_tree . khỏi tệp kê khai. Bởi vì nó sẽ bao gồm TẤT CẢ javascript trong tệp kê khai (làm cho có một biểu hiện khác nhau vô nghĩa)

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