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.
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:
You’ll need a corresponding route for individual records:get '/experiences.geojson', to: 'experiences#index_geojson'get '/experiences/:id.geojson', to: 'experiences#show_geojson' - Run
rails routesin 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_idinstead ofidin the route).
experiences_controller.rb) If the route checks out, the issue might be in your controller action:
- Did you create a dedicated
show_geojsonaction? 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.
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:
Make sure your JS is correctly grabbing that value:<div class="experience-show" data-experience-id="<%= @experience.id %>"></div>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.
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 yourExperiencemodel has ato_geojsonmethod 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.
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

