简体   繁体   中英

Rails confuses loading of assets by js for actual route and processes it as controller action

I'm trying to implement ion.sound in Rails 3.22 I can't get to have the sounds to be processed. I parametered ion.sound to fetch the sound assets in "/app/assets", in my js.coffee file like this :

# initialise plugin for ion sound
$ -> $.ionSound
    sounds: [
       { name: 'metal_plate' }
    ]
    path: '/app/assets/'
    preload: true
    multiplay: true
    volume: 0.5
# play sound
$ -> $.ionSound.play 'metal_plate'

but I noticed that the server trace indicates that it is conducting GETs on the sound paths and that Rails processes theses assets paths as controller actions, attempting to execute the corresponding actions which don't exist. Here is an exemple from the trace:

Started GET "/app/assets/metal_plate.aac?1476881187282" for 127.0.0.1 at 2016-10-19 14:46:27 +0200 Processing by DimensionsController#show as Parameters: {"1476881187282"=>nil, "client_id"=>"app", "produit_id"=>"assets", "id"=>"metal_plate"} Redirected to http://0.0.0.0:3000/log_in Filter chain halted as :signed_in_client rendered or redirected

so the result is a useless redirection and the sound asset isn't processed by ion.sound.

1) place your sound resources in /public/sounds

2) in your .js.coffee

#initialise plugin for ion sound
$ -> $.ionSound
    sounds: [
      { name: 'metal_plate' }
    ]
    path: '/sounds/'
    preload: true
    multiplay: true
    volume: 0.2

3) in your .js.erb file:

ion.sound.play("metal_plate")

URLs in the asset pipeline start with /assets , not /app/assets . The /app directory is not publicly accessible, you cannot link to files in there by their path.

If you have static files you want to serve to your front end, you should put them in /public , not app/assets .

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