You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mapbox单Experience展示视图GEOjson加载404问题排查求助

Hey there, sorry to hear you're stuck on that 404 when moving your Mapbox GeoJSON logic to the single Experience show view—let's walk through the most common culprits and how to debug them step by step.

1. First, Validate Your Routes (routes.rb)

404s almost always start with a mismatched request URL and defined route. Here's what to check:

  • Did you add a single Experience GeoJSON endpoint alongside your full list route? For example, if your full list route looks like this:
    get '/experiences.geojson', to: 'experiences#index_geojson'
    
    You’ll need a corresponding route for individual records:
    get '/experiences/:id.geojson', to: 'experiences#show_geojson'
    
  • Run rails routes in your terminal to confirm the route exists. Look for a line that matches the URL your frontend is requesting (you can check this in your browser’s Network tab).
  • Make sure the URL structure in your frontend matches exactly—no typos, missing slashes, or incorrect parameter names (e.g., using experience_id instead of id in the route).
2. Inspect Your Controller Logic (experiences_controller.rb)

If the route checks out, the issue might be in your controller action:

  • Did you create a dedicated show_geojson action? It should fetch the single Experience and render its GeoJSON, like this:
    def show_geojson
      @experience = Experience.find(params[:id])
      render json: @experience.to_geojson, content_type: 'application/geo+json'
    rescue ActiveRecord::RecordNotFound
      render json: { error: "Experience not found" }, status: :not_found
    end
    
  • Double-check that you’re using the correct parameter (params[:id]) to fetch the record. If your route uses a different param name (like :experience_id), adjust accordingly.
  • Ensure you’re not accidentally reusing the full list action (e.g., index_geojson) for the single record—this would try to fetch all experiences but fail if the URL includes an ID that doesn’t fit the route.
3. Debug the Frontend Request (Experiences.js)

Your frontend might be sending the wrong URL or missing the Experience ID entirely:

  • Check the Network tab in your browser’s dev tools: look for the GeoJSON request, note the full URL, and confirm it matches what you defined in routes.rb.
  • Verify how you’re fetching the Experience ID in your JS. For example, if you’re pulling it from a data attribute in the view:
    <div class="experience-show" data-experience-id="<%= @experience.id %>"></div>
    
    Make sure your JS is correctly grabbing that value:
    const experienceId = document.querySelector('.experience-show').dataset.experienceId;
    fetch(`/experiences/${experienceId}.geojson`)
      .then(response => response.json())
      .then(data => { /* add marker to Mapbox */ });
    
  • Avoid hardcoding IDs—always pull the ID dynamically from the page to prevent mismatches.
4. Confirm GeoJSON Generation Works for Single Records

If your route and controller are correct, the issue might be how you’re generating GeoJSON for a single Experience:

  • If you’re using a gem like rgeo-geojson, ensure your Experience model has a to_geojson method that works for individual instances (not just collections). For example:
    class Experience < ApplicationRecord
      def to_geojson
        feature = RGeo::GeoJSON::Feature.new(
          self.geometry, # Your spatial column (e.g., :location)
          self.id,
          { name: self.title, description: self.description } # Optional properties
        )
        RGeo::GeoJSON.encode(feature)
      end
    
  • Test the endpoint directly by visiting the URL in your browser (e.g., http://localhost:3000/experiences/1.geojson). If you get a 404 here, the issue is server-side; if you get valid GeoJSON, the problem is in your frontend Mapbox rendering logic.
5. Rule Out Authorization/Permissions

If you’re using an authorization gem like Pundit or CanCanCan, make sure the current user has permission to access the single Experience. Sometimes authorization errors are masked as 404s instead of 403s—try temporarily commenting out authorization checks to see if the request succeeds.


内容的提问来源于stack exchange,提问作者Justin Puda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:21:17