Meals#index ページで、表示されたマップの境界内にあるすべてのレストランをレンダリングするマップを表示したいと考えています。(各食事はレストランに属します。マップは実際に食事のリストとともに表示されます。AirBnB 検索ページのように、アパートではなく食べ物が表示されます)
Rails から React にデータを取得しようとすると、次のエラーが発生します。
不足しているキーワード: min_lat、max_lat、min_lng、max_lng
Map.js
fetchPlacesFromApi() {
this.setState({ places: [] })
fetch(`/meals?min_lng=${this.xMapBounds.min}&max_lng=${this.xMapBounds.max}&min_lat=${this.yMapBounds.min}&max_lat=${this.yMapBounds.max}`,
{ method: 'GET' })
.then((response) => response.json())
.then((response) => this.setState({ places: response }))
}
食事_コントローラー.rb
class MealsController < ApplicationController
def index
@restaurants = Restaurant.where(id: @meals.pluck(:restaurant_id))
places = @restaurants.look_at(search_params.to_h.symbolize_keys)
render json: places
end
private
def search_params
params.permit(:min_lng, :max_lng, :min_lat, :max_lat)
end
end
models/restaurant.rb
scope :by_longitude, -> (min, max) { min && max ? where('longitude >= :min AND longitude <= :max', min: min, max: max) : all }
scope :by_latitude, -> (min, max) { min && max ? where('latitude >= :min AND latitude <= :max', min: min, max: max) : all }
API_RESULTS_LIMIT = 100
def self.look_at(min_lat:, max_lat:, min_lng:, max_lng:)
by_latitude(min_lat, max_lat).
by_longitude(min_lng, max_lng).
limit(API_RESULTS_LIMIT)
end
食事/index.html.erb
<%= react_component("App", prerender: false) %>