Skip to content

Instantly share code, notes, and snippets.

@luismendes070
Created March 9, 2023 15:18
Show Gist options
  • Save luismendes070/c660f3661ce7251606369e75a306dc93 to your computer and use it in GitHub Desktop.
Save luismendes070/c660f3661ce7251606369e75a306dc93 to your computer and use it in GitHub Desktop.
dreams-map-postgres-ruby-on-rails
rails new dreams_map --database=postgresql
rails generate model Dream title:string description:text latitude:float longitude:float
rails db:migrate
rails generate controller Dreams
class DreamsController < ApplicationController
def index
@dreams = Dream.all
end
end
<div id="map"></div>
<script>
var map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors',
maxZoom: 18,
}).addTo(map);
<% @dreams.each do |dream| %>
L.marker([<%= dream.latitude %>, <%= dream.longitude %>]).addTo(map)
.bindPopup("<b><%= dream.title %></b><br><%= dream.description %>");
<% end %>
</script>
@luismendes070
Copy link
Author

Rails.application.routes.draw do
  resources :dreams

  root 'dreams#index'
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment