简体   繁体   中英

Rails: How to serve asset pipeline (just js) through a regular route?

I'm developing a project that consists in both a chrome extension and a spa. And, here is my problem: I have a lot of javascript that is common to both parts, what I'd like to do is:

  1. Serve the SPA through the regular asset pipeline (application.js)
  2. Serve common javascript to be consumed by the chrome extension with a regular route. (Say example.com/chrome-extension/application.js ).

Ideally this response (2) would have both data (from ruby) and the regular result of an asset pipeline.

File: app/views/chrome-extension/application.js.erb Global.data = { somekey: '<%= @some_value %>' } # append, inline, the same results that I'd get from `application.js` or, even better, from a second asset pipeline `chrome-extension.js`"

Any thoughts? :D

Found it :)

Rails.application.assets.find_asset('application.js').to_s is what I was looking for.

Found in this question

FYI I created two different files

application.js

//= require rails-ujs
//= require turbolinks
//= require_tree lib <- Magic here
//= require_tree spa
//= require index

lib.js (common to spa and chrome-extension)

//= require_tree ./models
//= require_tree ./resources
//= require_tree ./whatever

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM