简体   繁体   中英

JS particles injects into param :id (shows locally, not in heroku production)

I have this same issue locally and on production but at least in local, JS particles appears.

This is the git of the code:

    <div class="jumbo" id="particles-js"></div>
        <div id="particles-js"></div>
        <script src="particles.js"></script>
...

Then i get this same error local and production:

Started GET "/listings/particles.js" for xxx at 2019-03-21 21:23:12 -0400
Processing by ListingsController#show as JS
  Parameters: {"id"=>"particles"}
...
ActiveRecord::RecordNotFound (can't find record with friendly id: "particles"):

The issue isn't friednly ID because the same happens with or with out.

Now this error appears no matter what but in local at least the particles show.

What's up with this?

It looks like what is happening is that you have a resource called Listing which has a route that looks something like this /listings/:id .

When that page is rendered the html includes a script tag requesting particles.js which is a relative path, so the browser is making a request to /listings/particles.js . Rails is interpreting this as a request to the Listing resource and looking for a Listing with an id of particles which it cannot find.

To fix this, you have a couple options:

  1. require particles.js in your application.js file by adding the line below. This will include the particles.js javascript on every page of your website (that includes application.js )
//= require particles
  1. add a javascript_include_tag and include particles.js on the single html/erb page. This will only include particles.js on the specific view file but will require the user to download a separate Javascript file. To tell the Rails Asset Pipeline to include this file you will need to also modify config/initializers/assets.rb to ensure it is precompiled In your view:
<%= javascript_include_tag "particles" %>

In config/initializers/assets.rb :

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

Note: If you follow either of these steps, the Rails asset pipeline should minifiy the file for you when deploying to production (so you don't need to use particles.min.js).

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