39
loading...
This website collects cookies to deliver better user experience
rails new rails-react-google-maps --database=postgresql
--database=postgresql
option in the setup we tell the app to use PostgreSQL instead to save some time.rails db:create
rails db:migrate
rails db:seed
rails db:create
does what it says on the box - creates our database. This is an important step as without it we'll get an error when we try to run our server.rails db:migrate
is something we'll touch on soon but it's a part of that "rails magic" that makes database management simple. I'd recommend reading the Rails Guide on Active Record Migrations to learn more about how they work.rails db:seed
will mainly be used in the development phase of our project to create starter data we'll use to populate our app. We'll explore this more when we start creating instances of our Place model.rails db:create
Created database 'rails_react_google_maps_development'
Created database 'rails_react_google_maps_test'
rails server
in your terminal to start up your app. You should see the basic Rails welcome page.rails generate model Place name:string address:string latitude:float longitude:float
Running via Spring preloader in process 387002
invoke active_record
create db/migrate/20211104052039_create_places.rb
create app/models/place.rb
invoke test_unit
create test/models/place_test.rb
create test/fixtures/places.yml
create db/migrate/20211104052039_create_places.rb
rails db:migrate
== 20211104052039 CreatePlaces: migrating =====================================
-- create_table(:places)
-> 0.0106s
== 20211104052039 CreatePlaces: migrated (0.0107s) ============================
/rails-react-google-maps
) and open the Gemfile. Adding a gem to this file will make it available to anyone who clones your app with one easy command. To find out more about how the Gemfile works, check out the documentation.gem 'geocoder'
to your Gemfile anywhere above the development and test groups and save the file. Next, in your terminal run:bundle install
Using geocoder 1.6.7
. To create some necessary files for setup, execute the following command:rails generate geocoder:config
Running via Spring preloader in process 388633
create config/initializers/geocoder.rb
Geocoder.configure(
...
# Calculation options
units: :km,
...
)
/rails-react-google-maps/app/models
folder and open place.rb
and add the following lines inside the class:geocoded_by :address
after_validation :geocode
rails console
Place.create(address: '1600 Pennsylvania Avenue NW, Washington, DC 20500, United States', name: 'The White House')
TRANSACTION (0.3ms) BEGIN
Place Create (0.8ms) INSERT INTO "places" ("name", "address", "latitude", "longitude", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["name", "The White House"], ["address", "1600 Pennsylvania Avenue NW, Washington, DC 20500, United States"], ["latitude", 38.897699700000004], ["longitude", -77.03655315], ["created_at", "2021-11-04 08:22:20.077379"], ["updated_at", "2021-11-04 08:22:20.077379"]]
TRANSACTION (1.0ms) COMMIT
=> #<Place id: 3, name: "The White House", address: "1600 Pennsylvania Avenue NW, Washington, DC 20500,...", latitude: 38.897699700000004, longitude: -77.03655315, created_at: "2021-11-04 08:22:20.077379000 +0000", updated_at: "2021-11-04 08:22:20.077379000 +0000">
["latitude", 38.897699700000004]
and ["longitude", -77.03655315]
which means everything is working!rails-react-google-maps/db/
and paste the following into your seeds.rb file:places = [
{
name: 'The White House',
address: '1600 Pennsylvania Avenue NW, Washington, DC 20500, United States'
},
{
name: 'Washington Monument',
address: '2 15th St NW, Washington, DC 20024, United States'
},
{
name: 'Lincoln Memorial',
address: '2 Lincoln Memorial Cir NW, Washington, DC 20002, United States'
},
{
name: 'Washington National Cathedral',
address: '3101 Wisconsin Ave NW, Washington, DC 20016, United States'
},
{
name: 'Ronald Reagan Washington National Airport',
address: '2401 Smith Blvd, Arlington, VA 22202, United States'
}
]
puts 'Clearing seeds...'
Place.destroy_all
puts 'Seeds cleared.'
puts 'Seeding the database'
places.each do |place|
Place.create!(
name: place[:name],
address: place[:address]
)
end
puts "Created #{Place.all.count} places."
rails db:seed
Clearing seeds...
Seeds cleared.
Seeding the database
Created 5 places.
rails c
Running via Spring preloader in process 415433
Loading development environment (Rails 6.1.4.1)
2.6.6 :001 > Place.count
(0.7ms) SELECT COUNT(*) FROM "places"
=> 5
irb(main):002:0> Place.first
Place Load (0.3ms) SELECT "places".* FROM "places" ORDER BY "places"."id" ASC LIMIT $1 [["LIMIT", 1]]
=>
#<Place:0x000056403376b848
id: 2,
name: "The White House",
address: "1600 Pennsylvania Avenue NW, Washington, DC 20500, United States",
latitude: 38.897699700000004,
longitude: -77.03655315,
created_at: Fri, 05 Nov 2021 06:25:00.618439000 UTC +00:00,
updated_at: Fri, 05 Nov 2021 06:25:00.618439000 UTC +00:00>
rails generate
command again to create our controller and views. When generating a controller you can pass it the name of the methods you want to create inside it, which in turn creates the associated views. To get started, head back to your terminal and enter:rails generate controller places index
Running via Spring preloader in process 420964
create app/controllers/places_controller.rb
route get 'places/index'
invoke erb
create app/views/places
create app/views/places/index.html.erb
invoke test_unit
create test/controllers/places_controller_test.rb
invoke helper
create app/helpers/places_helper.rb
invoke test_unit
invoke assets
invoke scss
create app/assets/stylesheets/places.scss
create app/controllers/places_controller.rb
route get 'places/index'
places_controller.rb
you should see an empty method for our index action that will be revisited once we start rendering in our views:class PlacesController < ApplicationController
def index
end
end
invoke erb
create app/views/places
create app/views/places/index.html.erb
rails-react-google-maps/config/routes.rb
you'll see that the controller generator already gave us the route but we'll be cleaning it up using Rails resources and specifying what we want to have access to. At the same time we'll reroute the root (homepage) of our app to the index page:Rails.application.routes.draw do
root to: 'places#index'
resources :places, only: %i[index]
end
rails server
localhost:3000
in your browser and you should have a mostly empty page that looks like this: