ruby on rails: leaflet-rails not loading

后端 未结 3 1948
刺人心
刺人心 2021-01-15 06:28

Using RoR 4.1.4

I am trying to use the leaflet-rails gem. I followed the steps outlined in the github page, but when I try to load the map, I see

Re         


        
相关标签:
3条回答
  • 2021-01-15 06:43

    Turns out this is maybe some RoR specific issue.

    Many sites recommend to put the

    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    

    line which loads the javascripts, at the END of a file, in order to speed up page display. With the leaflet-rails gem, this isn't great, and will generate the problem described above. If putting the same line in the <head> section, the problem goes away...

    P.S. I still have problems with the gem though, now I get

    Error: No value provided for variable {s}
    

    If I substitute "http://{s}.tile.osm.org/{z}/{x}/{y}.png" with "http://a.tile.osm.org/{z}/{x}/{y}.png" it works...don't think it's meant to work that way though...but that's a different issue...

    0 讨论(0)
  • 2021-01-15 06:45

    I faced the same issue in rails.

    • leaflet works in development mode (bower managed)
    • Can't find variable L in production mode

    The following steps did not help.

    • asset "leaflet" (Bowerfile)
    • //= require leaflet/dist/leaflet.js (application.js)
    • *= require leaflet/dist/leaflet.css (application.css)
    • Rails.application.config.assets.precompile += ['leaflet/dist/leaflet.js', 'leaflet/dist/leaflet.css']
    • assets precompiled and uploaded to production public/assets/...

    Am I still missing something?

    [solution]

    This issue can be resolved by using CDN assets recommended by leaflet at http://leafletjs.com/examples/quick-start/

    • <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" />
    • <script src="https://unpkg.com/leaflet@1.0.1/dist/leaflet.js"></script>

    However, I would prefer to use local cached precompiled assets bundled at my app-server instead of relying on a public CDN, network bandwidths, and a lot more external things.

    0 讨论(0)
  • 2021-01-15 06:52

    Don't know if you were able to solve your issue. For me, I had forgotten to run:

    rake assets:precompile
    

    before deploying to production.

    0 讨论(0)
提交回复
热议问题